Skip to content

Commit 63f13a7

Browse files
committed
More solutions, 2 medium , 1 hard, 1 soft
1 parent 1fc29c9 commit 63f13a7

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

chapter_16/p16_21.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
def sum_swap(arr1: List[int], arr2: List[int]) -> tuple:
5+
if len(arr1) < len(arr2):
6+
return sum_swap(arr2, arr1)
7+
8+
posibs_set = set(arr2)
9+
target_change = abs(sum(arr1) - sum(arr2))
10+
11+
if target_change % 2 == 1:
12+
return None
13+
target_change //= 2
14+
15+
for x in arr1:
16+
if x-target_change in posibs_set:
17+
return (x, x-target_change)
18+
elif x+target_change in posibs_set:
19+
return (x, x + target_change)
20+
21+
return None
22+
23+
24+
if __name__ == "__main__":
25+
exs = [
26+
([4, 1, 2, 1, 1, 2], [3, 6, 3, 3]),
27+
([1, 2, 3, 4, 5], [1, 2, 3, 4, 4])
28+
]
29+
30+
for arr1, arr2 in exs:
31+
print(f"Swap {sum_swap(arr1, arr2)} to make {arr1} have same sum ar {arr2}")

chapter_16/p16_23.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import random
2+
from collections import defaultdict
3+
4+
STATISTICAL_SIGNIF_THRESHOLD = 10**6
5+
6+
7+
def rand5() -> int:
8+
return random.randint(0, 4)
9+
10+
11+
def rand7() -> int:
12+
res = rand5()*5 + rand5() # Get res in range 0-24
13+
while res >= 21: # Reroll until in 7-divisible range
14+
res = rand5()*5 + rand5()
15+
16+
return res % 7
17+
18+
19+
if __name__ == "__main__":
20+
results = defaultdict(lambda: 0)
21+
for _ in range(STATISTICAL_SIGNIF_THRESHOLD):
22+
results[rand7()] += 1
23+
24+
for x in sorted(results.keys()):
25+
print(f"Value {x} found times: {results[x]}")

chapter_17/p17_19.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
from typing import List
3+
import math
4+
import copy
5+
import random
6+
7+
TRIALS = 10
8+
9+
10+
def find_two_missing(arr: List[int], N: int) -> List[int]:
11+
# print(f"Array of len {len(arr)} is {arr}")
12+
all_sum = sum(range(1, N+1))
13+
all_quad_sum = sum(i**2 for i in range(1, N+1))
14+
15+
real_sum = sum(arr)
16+
real_quad_sum = sum(x**2 for x in arr)
17+
18+
delta_s = all_sum - real_sum
19+
delta_q = all_quad_sum - real_quad_sum
20+
21+
b = int((delta_s + math.sqrt(2*delta_q - delta_s**2)) / 2)
22+
a = int((delta_s ** 2 - delta_q) / (2*b))
23+
24+
return [a, b]
25+
26+
27+
def get_indexes_to_remove(N: int) -> tuple:
28+
a = random.randint(0, N-1)
29+
b = random.randint(0, N-1)
30+
while b == a:
31+
b = random.randint(0, N-1)
32+
33+
return (a, b)
34+
35+
36+
if __name__ == "__main__":
37+
N = 100
38+
all_arr = list(range(1, N+1))
39+
40+
for i in range(TRIALS):
41+
test_arr = copy.copy(all_arr)
42+
i1, i2 = get_indexes_to_remove(N)
43+
44+
a, b = test_arr[i1], test_arr[i2]
45+
46+
test_arr = [test_arr[i] for i in range(len(test_arr))
47+
if i not in {i1, i2}]
48+
49+
print(f"Missing from arr are {find_two_missing(test_arr,N)}, expected {a}, {b}")

chapter_4/p4_10.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from utils.graphs import ltbt, BiNode
2+
3+
4+
def check_same_tree(root1: BiNode, root2: BiNode) -> bool:
5+
if (not root1) and (not root2):
6+
return True
7+
elif (not root1) or (not root2):
8+
return False
9+
else:
10+
if root1.val != root2.val:
11+
return False
12+
else:
13+
return check_same_tree(root1.left, root2.left) \
14+
and check_same_tree(root1.right, root2.right)
15+
16+
17+
def check_sub_tree(t1: BiNode, t2: BiNode) -> bool:
18+
if not t1:
19+
return False
20+
if t1.val == t2.val:
21+
return check_same_tree(t1, t2)
22+
else:
23+
return check_sub_tree(t1.left, t2) or check_sub_tree(t1.right, t2)
24+
25+
26+
if __name__ == "__main__":
27+
tree1_list = [0, 10, 15, 20, 30, 25, 35] + [None] * 5 + [45]
28+
tree2_list = [10, 20, 30]
29+
30+
t1 = ltbt(tree1_list)
31+
t2 = ltbt(tree2_list)
32+
33+
print(f"Is t2 a subtree of t1 ? {check_sub_tree(t1,t2)}")
34+
print(f"Is t1 a subtree of t2 ? {check_sub_tree(t2,t1)}")

0 commit comments

Comments
 (0)