This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function counts(n) { | |
var s = '1' | |
for(var i = 0; i < n; i++) { | |
s = s.match(/(\d)(\1)*/g).map(x => x.length + x[0]).join('') | |
} | |
return s | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule S do | |
def anagram(xs, ys) do | |
count(xs) == count(ys) | |
end | |
defp count(xs), do: count(xs, %{}) | |
defp count(<<>>, seen), do: seen | |
defp count(<<x::utf8, rest::binary>>, seen) do | |
count(rest, Map.put(seen, x, (seen[x] || 0) + 1)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.function.Consumer; | |
public class BinaryTreeFlip { | |
public static void main(String[] args) throws Exception { | |
BinaryTree binaryTree = BinaryTree.buildTestTree(4, 2, 7, 1, 3, 6, 9); | |
//BinaryTree bt2 = BinaryTree.buildTestTree(4, 2, 7, 1, 3, 6, 9); | |
//BinaryTree bt2 = BinaryTree.buildTestTree(4, 2, 7, 1, 3, 6, 9, 10); | |
BinaryTree bt2 = BinaryTree.buildTestTree(4, 2, 8, 1, 3, 6, 9); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BinaryTree do | |
defstruct [:left, :right, :value] | |
def invert(%__module__{left: left, right: right} = root) do | |
%{root| left: invert(right), right: invert(left)} | |
end | |
def invert(nil), do: nil | |
def equals(nil, nil), do: true | |
def equals(%{value: a, left: l1, right: r1}, %{value: a, left: l2, right: r2}) do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function invert(xs) { | |
let j = 1 | |
while(j < xs.length) { | |
let start = j | |
let end = 2*j | |
while(end > start) { | |
swap(xs, start, end) | |
end-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BinaryTree do | |
defstruct [:left, :right, :value] | |
def invert(%__module__{left: left, right: right} = root) do | |
%{root| left: invert(right), right: invert(left)} | |
end | |
def invert(nil), do: nil | |
def test do | |
three = %BinaryTree{value: 3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function kaprekar(n) { | |
const asc = [].slice.call('000' + n).slice(-4).sort((a,b) => a - b).join('')|0 | |
const des = [].slice.call('000' + n).slice(-4).sort((a,b) => b - a).join('')|0 | |
return des - asc | |
} | |
function iterations(n) { | |
let i = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const elements = `1,H,Hydrogen,1.00794 | |
2,He,Helium,4.002602 | |
3,Li,Lithium,6.941 | |
4,Be,Beryllium,9.012182 | |
5,B,Boron,10.811 | |
6,C,Carbon,12.0107 | |
7,N,Nitrogen,14.0067 | |
8,O,Oxygen,15.9994 | |
9,F,Fluorine,18.9984032 | |
10,Ne,Neon,20.1797 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule S do | |
def fizzbuzz(n), do: fb(3, 5, 1, n, []) | |
def fb(_fizz, _buzz, i, i, r), do: r |> Enum.reverse | |
def fb(i, i, i, j, r) do | |
fb(i+3, i+5, i+1, j, ["FizzBuzz"|r]) | |
end | |
def fb(i, buzz, i, j, r) do | |
fb(i+3, buzz, i+1, j, ["Fizz"|r]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule S do | |
def test do | |
a = [[1, 3], [2, 6], [8, 10], [7, 11]] | |
[[1,6], [7,11]] = interval(a) | |
a = [[5,12],[8,10]] | |
[[5,12]] = interval(a) | |
a = [[1,6], [6,10]] | |
^a = interval(a) |