Last active
May 24, 2021 19:56
-
-
Save lucasew/702253601e9c45292cae041330db4fc8 to your computer and use it in GitHub Desktop.
THE TEST (entrypoint is then)
This file contains hidden or 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
# https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/ | |
with builtins; | |
rec { | |
theFn = l: | |
theFnRecur l 0; | |
theFnRecur = l: i: | |
let | |
len = length l; | |
in if len >= i then | |
let | |
this = diff l i; | |
next = theFnRecur l (i + 1); | |
in | |
if this < next | |
then this | |
else next | |
else 999999; | |
diff = l: i: | |
let | |
size = length l; | |
left = sumUntil l (i - 1); | |
right = sumUntil (reverse l) (size - i - 1); | |
value = left - right; | |
in abs value; | |
# assert i < size | |
sumUntil = l: i: | |
if i <= 0 | |
then (head l) | |
else | |
(head l) + (sumUntil (tail l) (i - 1)); | |
reverse = l: | |
if (length l) == 0 | |
then [] | |
else (reverse (tail l)) ++ [(head l)]; | |
# assert (theFn [3 1 2 4 3] == 1) | |
abs = n: if n < 0 then n * -1 else n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment