Last active
January 27, 2021 16:30
-
-
Save gubatron/ed966ea4e614d6733715376ad5cfb85f to your computer and use it in GitHub Desktop.
Pascal Triangle Generator in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pascal(n): | |
if n == 1: | |
return [ 1 ] | |
if n == 2: | |
return [ 1, 1 ] | |
prev = pascal(n-1) | |
results = [] | |
for i in range(n): | |
if i == 0: | |
continue | |
if i == n-1: | |
break | |
results.append(prev[i] + prev[i-1]) | |
return [1] + results + [1] | |
# functional style, no loops | |
def pascal_fp(n): | |
if n == 1: | |
return [ 1 ] | |
prev = pascal_fp(n-1) | |
return list(map(lambda x,y:x+y, [0] + prev, prev + [0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment