Teacup rim text: Difference between revisions
Content deleted Content added
Added C solution |
m →Python :: Functional: Tidying |
||
Line 1,235: | Line 1,235: | ||
''' |
''' |
||
def go(ws): |
def go(ws): |
||
def f(xs): |
|||
return [ |
|||
[snd(x) for x in xs] |
[snd(x) for x in xs] |
||
] if n <= len(xs) >= len(xs[0][0]) else [] |
] if n <= len(xs) >= len(xs[0][0]) else [] |
||
)( |
return concatMap(f)(groupBy(fst)(sorted( |
||
[(''.join(sorted(w)), w) for w in ws], |
|||
key=fst |
|||
⚫ | |||
[(''.join(sorted(w)), w) for w in ws], |
|||
⚫ | |||
⚫ | |||
) |
|||
) |
|||
⚫ | |||
return lambda ws: go(ws) |
|||
Line 1,285: | Line 1,281: | ||
(0, True, rotated(w)) |
(0, True, rotated(w)) |
||
)[1] |
)[1] |
||
⚫ | |||
⚫ | |||
Line 1,306: | Line 1,301: | ||
(using an empty list to represent computational failure). |
(using an empty list to represent computational failure). |
||
''' |
''' |
||
def go(xs): |
|||
chain.from_iterable(map(f, xs)) |
return chain.from_iterable(map(f, xs)) |
||
return go |
|||
Line 1,323: | Line 1,318: | ||
in terms of the key function f. |
in terms of the key function f. |
||
''' |
''' |
||
def go(xs): |
|||
⚫ | |||
list(x[1]) for x in groupby(xs, key=f) |
|||
⚫ | |||
] |
] |
||
return go |
|||
Line 1,377: | Line 1,374: | ||
over a start value of x. |
over a start value of x. |
||
''' |
''' |
||
def go(f |
def go(f): |
||
def g( |
def g(x): |
||
def h(a, i): |
|||
v = f(a) if i else x |
|||
return |
return (v, v) |
||
return mapAccumL(h)(x)( |
|||
range(0, 1 + n) |
|||
)[1] |
|||
return g |
|||
return go |
|||
Line 1,392: | Line 1,391: | ||
The initial seed value is x. |
The initial seed value is x. |
||
''' |
''' |
||
def go(f |
def go(f): |
||
def g(x): |
|||
v = x |
|||
while not p(v): |
|||
v = f(v) |
|||
return v |
|||
return g |
|||
return go |
|||