Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Digital-root/Python/digital-root-1.py
Normal file
13
Task/Digital-root/Python/digital-root-1.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def digital_root (n):
|
||||
ap = 0
|
||||
n = abs(int(n))
|
||||
while n >= 10:
|
||||
n = sum(int(digit) for digit in str(n))
|
||||
ap += 1
|
||||
return ap, n
|
||||
|
||||
if __name__ == '__main__':
|
||||
for n in [627615, 39390, 588225, 393900588225, 55]:
|
||||
persistance, root = digital_root(n)
|
||||
print("%12i has additive persistance %2i and digital root %i."
|
||||
% (n, persistance, root))
|
||||
64
Task/Digital-root/Python/digital-root-2.py
Normal file
64
Task/Digital-root/Python/digital-root-2.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from functools import (reduce)
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
print (
|
||||
tabulated(digitalRoot)(
|
||||
'Integer -> (additive persistence, digital root):'
|
||||
)([627615, 39390, 588225, 393900588225, 55])
|
||||
)
|
||||
|
||||
|
||||
# digitalRoot :: Int -> (Int, Int)
|
||||
def digitalRoot(n):
|
||||
'''Integer -> (additive persistence, digital root)'''
|
||||
|
||||
# f :: (Int, Int) -> (Int, Int)
|
||||
def f(pn):
|
||||
p, n = pn
|
||||
return (
|
||||
1 + p,
|
||||
reduce(lambda a, x: a + int(x), str(n), 0)
|
||||
)
|
||||
|
||||
# p :: (Int , Int) -> Bool
|
||||
def p(pn):
|
||||
return 10 > pn[1]
|
||||
|
||||
return until(p)(f)(
|
||||
(0, abs(int(n)))
|
||||
)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g):
|
||||
return lambda f: lambda x: g(f(x))
|
||||
|
||||
|
||||
# tabulated :: (a -> b) -> String -> String
|
||||
def tabulated(f):
|
||||
'''function -> heading -> input List -> tabulated output string'''
|
||||
def go(s, xs):
|
||||
fw = compose(len)(str)
|
||||
w = fw(max(xs, key=fw))
|
||||
return s + '\n' + '\n'.join(list(map(
|
||||
lambda x: str(x).rjust(w, ' ') + ' -> ' + str(f(x)), xs
|
||||
)))
|
||||
return lambda s: lambda xs: go(s, xs)
|
||||
|
||||
|
||||
# until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
def until(p):
|
||||
def go(f, x):
|
||||
v = x
|
||||
while not p(v):
|
||||
v = f(v)
|
||||
return v
|
||||
return lambda f: lambda x: go(f, x)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue