Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
87 changes: 79 additions & 8 deletions 6_18_2024_catalan/catalan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

def parenthesizations(n):
"""

Returns a set of all possible parenthesizations of length n.

Parameters:
Expand All @@ -17,9 +18,25 @@ def parenthesizations(n):
if n == 0:
return {""}
else:
# TODO
pass
output = set()
parenthesizations_helper("", n, n, output)
return output

"""
res - result string
open - number of open parenthesis
close - number of close parenthesis
output - output set
"""
def parenthesizations_helper(res, open, close, output):
if open == 0 and close == 0:
output.add(res)
else:
if open > 0:
parenthesizations_helper(res + "(", open - 1, close, output)
if close != open:
parenthesizations_helper(res + ")", open, close - 1, output)

def product_orders(n):
"""
Returns a set of all possible ways to multiply of n elements.
Expand All @@ -41,8 +58,23 @@ def product_orders(n):
elif n == 2:
return {"?*?"}
else:
# TODO
pass
output = set()
return product_orders_helper(n, n)


def product_orders_helper(n, overall_length):
if n == 1:
return {"?"}
output = set()
for i in range(1, n):
for j in product_orders_helper(i, overall_length):
for k in product_orders_helper(n - i, overall_length):
new_string = j + "*" + k
if new_string[0] == "?" or new_string[len(new_string) - 1] == "?":
if n < overall_length:
new_string = "(" + new_string + ")"
output.add(new_string)
return output

def permutations_avoiding_231(n):
"""
Expand All @@ -58,11 +90,32 @@ def permutations_avoiding_231(n):
>>> permutations_avoiding_231(4)
{(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (3, 1, 2, 4), (3, 2, 1, 4), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 3, 1, 2), (4, 3, 2, 1)}
"""

if n < 3:
return set(itertools.permutations(range(1, n+1)))
else:
# TODO
pass
perms = itertools.permutations(range(1, n+1))
output = set()
found = False
for perm in perms:
found = False
for i in range(n - 2):
first = perm[i]
for j in range(i + 1, n - 1):
second = perm[j]
for k in range(j + 1, n):
third = perm[k]
if second > first and first > third:
found = True
break
if found:
break
if found:
break
if not found:
output.add(perm)
return output


def triangulations(n):
"""
Expand All @@ -84,5 +137,23 @@ def triangulations(n):
elif n == 3:
return {tuple()}
else:
pass
# TODO
vertices = list(range(n - 1))
output = set()
for i in range (n - 2):
output = output.union(triangulation_helper(i, vertices))
return output


def triangulation_helper(start, vertices):
length = len(vertices)
vertices = sorted(vertices)
output = set()
if len(vertices) == 3:
return {((vertices[(start) % length], vertices[(start + 1) % length]), (vertices[start % length], vertices[(start + 2) % length]))}
else:
triangle = ((vertices[(start) % length], vertices[(start + 1) % length]), (vertices[start % length], vertices[(start + 2) % length]))
del vertices[(start + 1) % length]
output.add(triangle)
return output.union(triangulation_helper(start, vertices))


127 changes: 114 additions & 13 deletions 6_18_2024_catalan/main.ipynb

Large diffs are not rendered by default.