Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Pascals-triangle/Python/pascals-triangle-1.py
Normal file
9
Task/Pascals-triangle/Python/pascals-triangle-1.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def pascal(n):
|
||||
"""Prints out n rows of Pascal's triangle.
|
||||
It returns False for failure and True for success."""
|
||||
row = [1]
|
||||
k = [0]
|
||||
for x in range(max(n,0)):
|
||||
print row
|
||||
row=[l+r for l,r in zip(row+k,k+row)]
|
||||
return n>=1
|
||||
17
Task/Pascals-triangle/Python/pascals-triangle-2.py
Normal file
17
Task/Pascals-triangle/Python/pascals-triangle-2.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
def scan(op, seq, it):
|
||||
a = []
|
||||
result = it
|
||||
a.append(it)
|
||||
for x in seq:
|
||||
result = op(result, x)
|
||||
a.append(result)
|
||||
return a
|
||||
|
||||
def pascal(n):
|
||||
def nextrow(row, x):
|
||||
return [l+r for l,r in zip(row+[0,],[0,]+row)]
|
||||
|
||||
return scan(nextrow, range(n-1), [1,])
|
||||
|
||||
for row in pascal(4):
|
||||
print(row)
|
||||
97
Task/Pascals-triangle/Python/pascals-triangle-3.py
Normal file
97
Task/Pascals-triangle/Python/pascals-triangle-3.py
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
'''Pascal's triangle'''
|
||||
|
||||
from itertools import (accumulate, chain, islice)
|
||||
from operator import (add)
|
||||
|
||||
|
||||
# nextPascal :: [Int] -> [Int]
|
||||
def nextPascal(xs):
|
||||
'''A row of Pascal's triangle
|
||||
derived from a preceding row.'''
|
||||
return list(
|
||||
map(add, [0] + xs, xs + [0])
|
||||
)
|
||||
|
||||
|
||||
# pascalTriangle :: Generator [[Int]]
|
||||
def pascalTriangle():
|
||||
'''A non-finite stream of
|
||||
Pascal's triangle rows.'''
|
||||
return iterate(nextPascal)([1])
|
||||
|
||||
|
||||
# finitePascalRows :: Int -> [[Int]]
|
||||
def finitePascalRows(n):
|
||||
'''The first n rows of Pascal's triangle.'''
|
||||
return accumulate(
|
||||
chain(
|
||||
[[1]], range(1, n)
|
||||
),
|
||||
lambda a, _: nextPascal(a)
|
||||
)
|
||||
|
||||
|
||||
# ------------------------ TESTS -------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test of two different approaches:
|
||||
- taking from a non-finite stream of rows,
|
||||
- or constructing a finite list of rows.'''
|
||||
print('\n'.join(map(
|
||||
showPascal,
|
||||
[
|
||||
islice(
|
||||
pascalTriangle(), # Non finite,
|
||||
7
|
||||
),
|
||||
finitePascalRows(7) # finite.
|
||||
]
|
||||
)))
|
||||
|
||||
|
||||
# showPascal :: [[Int]] -> String
|
||||
def showPascal(xs):
|
||||
'''Stringification of a list of
|
||||
Pascal triangle rows.'''
|
||||
ys = list(xs)
|
||||
|
||||
def align(w):
|
||||
return lambda ns: center(w)(
|
||||
' '
|
||||
)(' '.join(map(str, ns)))
|
||||
|
||||
w = len(' '.join((map(str, ys[-1]))))
|
||||
return '\n'.join(map(align(w), ys))
|
||||
|
||||
|
||||
# ----------------------- GENERIC ------------------------
|
||||
|
||||
# center :: Int -> Char -> String -> String
|
||||
def center(n):
|
||||
'''String s padded with c to approximate centre,
|
||||
fitting in but not truncated to width n.'''
|
||||
def go(c, s):
|
||||
qr = divmod(n - len(s), 2)
|
||||
q = qr[0]
|
||||
return (q * c) + s + ((q + qr[1]) * c)
|
||||
|
||||
return lambda c: lambda s: go(c, s)
|
||||
|
||||
|
||||
# iterate :: (a -> a) -> a -> Gen [a]
|
||||
def iterate(f):
|
||||
'''An infinite list of repeated
|
||||
applications of f to x.
|
||||
'''
|
||||
def go(x):
|
||||
v = x
|
||||
while True:
|
||||
yield v
|
||||
v = f(v)
|
||||
|
||||
return go
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue