Last active
April 1, 2018 17:27
-
-
Save broerjuang/97aa4f6a7e6329a2758c46fa33fef9c6 to your computer and use it in GitHub Desktop.
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
module Utils = { | |
let rec repeat = (whatToRepeat, howManyTimes) => | |
switch howManyTimes { | |
| 0 => "" | |
| someNumber when howManyTimes > 0 => whatToRepeat ++ repeat(whatToRepeat, howManyTimes - 1) | |
| _ => "" | |
}; | |
let rec makePairFromList = (xs) => | |
switch xs { | |
| [] => [] | |
| [a] => raise(Exit) | |
| [f, s, ...tl] => [(f, s), ...makePairFromList(tl)] | |
}; | |
let rec range = (x) => | |
switch x { | |
| 0 => [] | |
| x when x > 0 => List.append([x], range(x - 1)) | |
| _ => [] | |
}; | |
let splitBy = (by, str) => Str.split(Str.regexp(by), str); | |
}; | |
/* it assumes that list is int */ | |
let getAverage = (xs) => (List.fold_left((+), 0, xs) |> float) /. (List.length(xs) |> float); | |
/* Digit Explode */ | |
let digitsExplode = (str) => | |
Utils.splitBy("", str) |> List.map((x) => Utils.repeat(x, int_of_string(x))); | |
/* Parse */ | |
type parse = { | |
name: string, | |
value: string | |
}; | |
let parse = (str) => | |
Utils.splitBy(" ", str) | |
|> Utils.makePairFromList | |
|> List.map((x) => {name: fst(x), value: snd(x)}); | |
let sumMultiplesBelow = (x) => | |
Utils.range(x - 1) | |
|> List.filter((x) => x mod 3 === 0 || x mod 5 === 0) | |
|> List.fold_left((+), 0); | |
let getDivisors = (x) => Utils.range(x) |> List.filter((id) => x mod id === 0) |> List.length; | |
let isAnagram = (s1, s2) => { | |
let sortedS1 = List.sort(compare, Utils.splitBy("", s1)); | |
let sortedS2 = List.sort(compare, Utils.splitBy("", s2)); | |
sortedS1 === sortedS2 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment