tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue