Created
January 25, 2017 18:36
-
-
Save iczero/35a87f3537aeb01833bf53f4e1a3f98c to your computer and use it in GitHub Desktop.
Pascal's Triangle in python3
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
#!/usr/bin/env python3 | |
"""Calculate Pascal's Triangle""" | |
import sys | |
def get_triangle(num): | |
"""return 2D list of triangle""" | |
ret = [[1]] | |
for i in range(1, num + 1): | |
sys.stdout.write('\rrow: '+str(i)) | |
row = [] | |
for j in range(0, i+1): | |
if j == 0 or j == i: | |
row.append(1) # first or last position | |
else: | |
res = ret[i-1][j-1]+ret[i-1][j] | |
row.append(res) | |
ret.append(row) | |
sys.stdout.write('\n') | |
return ret | |
def main(): | |
"""Get number of rows to calculate from the user and | |
show a pretty triangle""" | |
try: | |
num = int(input('Number of rows: ')) | |
except ValueError: | |
print('Please input an integer') | |
exit(1) | |
triangle = get_triangle(num) | |
length = len(triangle) - 1 | |
if length > 100: | |
return print('too large, not displaying') | |
for i in range(0, length): | |
row = triangle[i] | |
print(' '*(length-i)+(' '.join(str(j) for j in row))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment