Steady squares: Difference between revisions
→Python :: Functional: Updated first version to use concatMap and .endswith
(→Python :: Functional: Updated first version to use concatMap and .endswith) |
|||
Line 946:
<lang python>'''Steady squares'''
from itertools import
#
def
'''
containing a tuple of the decimal strings of (x, x^2)
'''
return [(s, s2)] if s2.endswith(s) else []
)(▼
▲ str(x ** 2)
)▼
#
# main :: IO ()
def main():
'''Roots of numbers with steady squares up to 10000
'''
xs = concatMap(steadyPair)(ns)
w, w2 =
print([n for n in ns if steadyPair(n)])
print()
'\n'.join([
f'{s.rjust(w, " ")} -> {s2.rjust(w2, " ")}'
▲ ])
)
▲ for x in xs:
# ----------------------- GENERIC ------------------------
#
def
'''
mapped.
The list monad can be derived by using a function f
which wraps its output in a list, (using an empty
list to represent computational failure).
'''
def go(
return go
Line 990 ⟶ 998:
main()</lang>
{{Out}}
<pre>[1, 5, 6, 25, 76, 376, 625, 9376]
625 -> 390625
9376 -> 87909376</pre>
|