Created
October 7, 2022 11:13
-
-
Save fgshun/10b00f6544e7d9ceed60494c44d49da5 to your computer and use it in GitHub Desktop.
Pascal's triangle
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
class PascalTriangle: | |
def __init__(self, n): | |
t = [[1.0]] | |
for i in range(2, n + 1): | |
row = t[i - 2] | |
temp = [1.0] | |
for j in range(i - 2): | |
# temp.append(t[i - 2][j] + t[i - 2][j + 1]) | |
# temp.append(sum(t[i - 2][j:j + 2])) | |
temp.append(sum(row[j:j + 2])) | |
temp.append(1.0) | |
t.append(temp) | |
self.triangle = t | |
def comb(self, n, m): | |
return self.triangle[n][m] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment