Finish week 1 problem set and merge sort in Rust

This commit is contained in:
2020-11-16 20:24:01 -05:00
parent 79a7a05b21
commit 60dfe69601
4 changed files with 100096 additions and 8 deletions

37
py/week_1_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]))

50
py/week_1_set_1.py Normal file
View File

@@ -0,0 +1,50 @@
import matplotlib.pyplot as plt
import numpy as np
n = np.arange(1, 1000, 1)
# Version 1
a = 2**np.log(n)
b = 2**2**np.log(n)
c = n**(5/2)
d = 2**n**2
e = (n**2)*np.log(n)
# plt.plot(n, a, 'b', n, b, 'y') # a < b
# plt.plot(n, b, 'b', n, c, 'y') # b > c
# plt.plot(n, a, 'b', n, c, 'y') # a < c
# plt.plot(n, c, 'b', n, d, 'y') # c << d
# plt.plot(n, c, 'b', n, e, 'y') # c > e
# plt.plot(n, e, 'b', n, b, 'y') # e < b
# plt.plot(n, e, 'b', n, a, 'y') # e > a
# a < e < c < b < d
n = np.arange(1, 1000, 1)
# n = 1000
a = np.sqrt(n)
b = 10**n
c = n**1.5
d = 2**np.sqrt(np.log(n))
e = n**5/3
# print(a, b, c, d, e)
# d < a < c < e < b
# print(d < a, a < c, c < e, e < b)
plt.plot(n, a, 'b', n, d, 'y') # a > d
plt.show()
"""
1) n*log(n)
2) True
3) Yes if and Sometimes yes
4) O(nk**2)
5) :/ Tried:
ecadb
abecd
"""