Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different Pascal's triangle implementation #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Patterns/pascals_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from math import factorial
from itertools import combinations

#This is a recursive implementation for generating Pascal's triangle.
def recursive(n):
def recursive_row(n,k):
if k == 0 or k == n:
return 1
return recursive_row(n-1, k-1) + recursive_row(n-1, k);
triangle = [];
for row in range(n):
curr_row = [];
for k in range(row+1):
curr_row.append(recursive_row(row, k))
triangle.append(curr_row)

return triangle

#This is the typical definition of Pascal's triangle.
def bruteForce_sumLast(n):
triangle = []
for row in range(n):
curr_row = []
for i in range(row+1):
if i == 0 or i == row:
curr_row.append(1);
else:
curr_row.append(triangle[-1][i-1]+triangle[-1][i])
triangle.append(curr_row)
return triangle

#We define the n choose k function: https://en.wikipedia.org/wiki/Combination
#This will be useful in the next function.
def nchoosek(n,k):
#There are multiple ways of computing this aswell!
#For example, it can be computed recursively or via the definition nCk = n!/(k!*(n-k)!)
return len(list(combinations(range(n),k)))

#This is the definition of the Pascal's triangle using combinatorial numbers.
def bruteForce_factorial(n):
triangle = []
for row in range(n):
curr_row = []
for col in range(row+1):
curr_row.append( nchoosek(row, col) )
triangle.append(curr_row)
return triangle


#This algorithm is taken from user MortalViews in StackExchange from the post
#https://stackoverflow.com/questions/24093387/pascals-triangle-for-python
#This is just for visualization purposes!
#Just enter the output of any of the above functions and it will work.
def draw_beautiful(ps):
max = len(' '.join(map(str,ps[-1])))
for p in ps:
print(' '.join(map(str,p)).center(max)+'\n')
15 changes: 15 additions & 0 deletions Patterns/sierpinskys_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import matplotlib.pylab as plt
from random import randint

def midpoint(p1, p2):
return ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)

width, height = 1, 1
p, q, r = (width/2, height), (0, 0), (width, 0); #This three points form a triangle
triangle = [p,q,r]
mid_point = (width/2, height/2)

for _ in range(5000): #if you increase this number, then the fractal will be more defined!
mid_point = midpoint(mid_point, triangle[randint(0,2)])
plt.plot(mid_point[0], mid_point[1], 'm.', markersize=2)
plt.show()