Created
June 15, 2016 12:23
-
-
Save stamaniorec/092be58e8d2166be01da5ffa893ecb37 to your computer and use it in GitHub Desktop.
Pascal triangle
This file contains hidden or 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
| from __future__ import print_function | |
| def sum_one_to_n(n): | |
| return (n * (n + 1)) / 2 | |
| def get_index(row, col): | |
| return sum_one_to_n(row) + col | |
| def get_value(row, col, l): | |
| if col < 0: | |
| return 0 | |
| return l[get_index(row, col)] | |
| n = 5 | |
| how_many_elements = sum_one_to_n(n) | |
| l = [None] * how_many_elements | |
| l[0] = 1 | |
| row = 1 | |
| col = 0 | |
| for _ in range(1, how_many_elements): | |
| l[get_index(row, col)] = get_value(row-1, col-1, l) + get_value(row-1, col, l) | |
| col += 1 | |
| if col > row: | |
| row = col | |
| col = 0 | |
| # printing works prettily for n < 6 | |
| dots = n - 1 | |
| index = 0 | |
| for i in range(0, n): | |
| for _ in range(0, dots): | |
| print('.', end='') | |
| for k in range(0, i+1): | |
| print(l[index], end='') | |
| index += 1 | |
| if k >= i: | |
| break | |
| print('_', end='') | |
| for _ in range(0, dots): | |
| print('.', end='') | |
| dots -= 1 | |
| print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment