Created
December 26, 2019 18:09
-
-
Save gpalazuelosg/f1db17e4cacbd013b6ec154d3dfe72b2 to your computer and use it in GitHub Desktop.
Pascal's triangle - my algorithm coded 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
# what is Pascal's triangle? | |
# https://en.wikipedia.org/wiki/Pascal%27s_triangle | |
number_of_rows = 11 | |
def print_triangle_row(rows:int, current_row:int): | |
one_line= "" | |
# iterate on current_line | |
for column in range(0,current_row): | |
value = calculate_position(rows,current_row,column) | |
one_line += f"{value}\t" | |
print(f"{one_line}") | |
if current_row < rows: | |
print_triangle_row(rows,1+current_row) | |
else: | |
return | |
def calculate_position(rows:int, current_row:int, current_column:int): | |
if current_row <= 1 or current_column == 0 or current_column == (current_row-1): | |
return 1 | |
return calculate_position(rows, current_row-1, current_column-1) + calculate_position(rows, current_row-1, current_column) | |
def pascal_triangle(rows:int): | |
if rows > 0: | |
print_triangle_row(rows,1) | |
else: | |
print("Error!") | |
pascal_triangle(number_of_rows) | |
print("terminamos...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment