Clean up and get ready for two sum assignment

This commit is contained in:
2020-12-12 10:19:24 -05:00
parent e9d0620ab5
commit 99ef6d4d1c
10 changed files with 49 additions and 8 deletions

16
misc/birthday_problem.py Normal file
View File

@@ -0,0 +1,16 @@
def p_all_different(n):
p = 1
free_days = 365
days_year = 365
for i in range(0, n):
p = p * (free_days / days_year)
free_days -= 1
return p
if __name__ == "__main__":
for i in range(1, 30):
print(f"p_all_different({i})={p_all_different(i)}")
if p_all_different(i) < 0.5:
break

BIN
misc/dijkstra_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 962 KiB

BIN
misc/dijkstra_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 KiB

BIN
misc/dijkstra_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 779 KiB

37
misc/merge_sort.py Normal file
View File

@@ -0,0 +1,37 @@
def merge(a, b):
"""Merges two sorted lists"""
i, j = 0, 0
r = []
while i < len(a) and j < len(b):
if a[i] < b[j]:
r.append(a[i])
i += 1
else:
r.append(b[j])
j += 1
while i < len(a):
r.append(a[i])
i += 1
while j < len(b):
r.append(b[j])
j += 1
return r
def merge_sort(v):
length = len(v)
if length <= 1:
return v
half_length = length // 2
a = merge_sort(v[:half_length])
b = merge_sort(v[half_length:])
return merge(a, b)
if __name__ == "__main__":
print(merge_sort([42, 12, 3, 1, 5]))