Equilibrium index: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied the Prelude functions version) |
m (→Python :: In terms of itertools.accumulate: Tidied.) |
||
Line 2,101: | Line 2,101: | ||
equals the sum of values to the right.''' |
equals the sum of values to the right.''' |
||
def go(xs): |
def go(xs): |
||
'''Left scan from accumulate, |
'''Left scan from accumulate, |
||
right scan derived from left |
|||
⚫ | |||
ls = list(accumulate(xs)) |
ls = list(accumulate(xs)) |
||
n = ls[-1] |
n = ls[-1] |
||
return [ |
return [ |
||
i for (i, (x, y)) in enumerate(zip( |
|||
ls, |
|||
[n] + [n - x for x in ls[0:-1]] |
|||
)) if x == y |
|||
] |
|||
return go(xs) if xs else [] |
return go(xs) if xs else [] |
||
# |
# ------------------------- TEST ------------------------- |
||
# main :: IO () |
# main :: IO () |
||
def main(): |
def main(): |
||
Line 2,129: | Line 2,133: | ||
# |
# ----------------------- GENERIC ------------------------ |
||
# tabulated :: String -> (a -> b) -> [a] -> String |
# tabulated :: String -> (a -> b) -> [a] -> String |
||
def tabulated(s): |
def tabulated(s): |
||
'''heading -> function -> input List |
'''heading -> function -> input List |
||
-> tabulated output string |
|||
⚫ | |||
''' |
|||
⚫ | |||
def width(x): |
def width(x): |
||
return len(str(x)) |
return len(str(x)) |
||
def cols(xs): |
|||
w = width(max(xs, key=width)) |
|||
return s + '\n' + '\n'.join([ |
|||
str(x).rjust(w, ' ') + ' -> ' + str(f(x)) |
|||
⚫ | |||
for x in xs |
|||
]) |
|||
return cols |
|||
return go |
|||