Skip to content

Commit 833d49e

Browse files
Add files via upload
0 parents  commit 833d49e

File tree

81 files changed

+3425
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3425
-0
lines changed

Broadway_Tower.jpg

249 KB
Loading

Listing_1_1_factorial.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def factorial(n):
2+
if n == 1 or n == 0:
3+
return 1
4+
return factorial(n - 1) * n
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def sum_n_integers(n):
2+
"""Given input n, sum all nonnegative integers up to n
3+
"""
4+
if n == 0 or n == 1:
5+
return n
6+
return sum_n_integers(n - 1) + n
7+
8+
def sum_arr_v1(nums):
9+
if len(nums) == 1:
10+
return nums[0]
11+
return nums[0] + sum_arr_v1(nums[1:])
12+
13+
def sum_arr_v2(nums):
14+
if len(nums) == 1:
15+
return nums[-1]
16+
return nums[-1] + sum_arr_v2(nums[:-1])
17+
18+
if __name__ == "__main__":
19+
assert sum_n_integers(5) == 15
20+
assert sum_arr_v1([1,2,3,4,5]) == 15
21+
assert sum_arr_v2([1,2,3,4,5]) == 15

Listing_1_3_number_unique_paths.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def count_unique_paths(m, n):
2+
if m == 1 or n == 1:
3+
return 1
4+
return count_unique_paths(m-1, n) + count_unique_paths(m, n-1)
5+
6+
if __name__ == "__main__":
7+
print (count_unique_paths(7,4))

Listing_1_4_h_tree_generation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import math
2+
import matplotlib.pyplot as plt
3+
plt.figure()
4+
def draw_line(x1, y1, x2, y2):
5+
plt.plot([x1, x2], [y1, y2], color = 'r', lw = 2)
6+
7+
def draw_h_tree(x0, y0, length, level, factor):
8+
if level == 0:
9+
return
10+
11+
coord_c_x = x0 - length / 2.
12+
coord_c_y = y0
13+
coord_d_x = x0 + length / 2.
14+
coord_d_y = y0
15+
16+
coord_a_x = x0 - length / 2.
17+
coord_a_y = y0 + length / 2.
18+
coord_e_x = x0 - length / 2.
19+
coord_e_y = y0 - length / 2.
20+
coord_b_x = x0 + length / 2.
21+
coord_b_y = y0 + length / 2.
22+
coord_f_x = x0 + length / 2.
23+
coord_f_y = y0 - length / 2.
24+
25+
draw_line(coord_c_x, coord_c_y, coord_d_x, coord_d_y)
26+
draw_line(coord_a_x, coord_a_y, coord_e_x, coord_e_y)
27+
draw_line(coord_b_x, coord_b_y, coord_f_x, coord_f_y)
28+
29+
length /= math.sqrt(factor)
30+
level -= 1
31+
draw_h_tree(coord_a_x, coord_a_y, length, level, factor)
32+
draw_h_tree(coord_b_x, coord_b_y, length, level, factor)
33+
draw_h_tree(coord_e_x, coord_e_y, length, level, factor)
34+
draw_h_tree(coord_f_x, coord_f_y, length, level, factor)
35+
36+
if __name__ == "__main__":
37+
draw_h_tree(0, 0, 2, 5, 4)
38+
plt.tight_layout()
39+
plt.show()

Listing_1_5_tower_of_hanoi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def tower_of_hanoi(num_disks, from_pole, with_pole, to_pole):
2+
if num_disks == 1:
3+
print (f"Move disk 1 from {from_pole} to {to_pole}")
4+
else:
5+
tower_of_hanoi(num_disks - 1,from_pole, to_pole, with_pole)
6+
print (f"Move disk {num_disks} from {from_pole} to {to_pole}")
7+
tower_of_hanoi(num_disks - 1, with_pole, from_pole, to_pole)
8+
9+
if __name__ == "__main__":
10+
tower_of_hanoi(4, "A", "B", "C")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class TreeNode:
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
def pre_order_traversal(root):
8+
sequence = []
9+
def preorder_helper(node):
10+
if node:
11+
sequence.append(node.value)
12+
preorder_helper(node.left)
13+
preorder_helper(node.right)
14+
preorder_helper(root)
15+
return sequence
16+
17+
if __name__ == "__main__":
18+
root = TreeNode(1)
19+
root.left = TreeNode(2)
20+
root.right = TreeNode(3)
21+
root.left.left = TreeNode(4)
22+
root.left.right = TreeNode(5)
23+
root.right.left = TreeNode(6)
24+
root.right.right = TreeNode(7)
25+
print("Preorder traversal of the tree:", pre_order_traversal(root))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class TreeNode:
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
def in_order_traversal(root):
8+
sequence = []
9+
def inorder_helper(node):
10+
if node:
11+
inorder_helper(node.left)
12+
sequence.append(node.value)
13+
inorder_helper(node.right)
14+
inorder_helper(root)
15+
return sequence
16+
17+
def post_order_traversal(root):
18+
sequence = []
19+
def postorder_helper(node):
20+
if node:
21+
postorder_helper(node.left)
22+
postorder_helper(node.right)
23+
sequence.append(node.value)
24+
postorder_helper(root)
25+
return sequence
26+
27+
if __name__ == "__main__":
28+
root = TreeNode(1)
29+
root.left = TreeNode(2)
30+
root.right = TreeNode(3)
31+
root.left.left = TreeNode(4)
32+
root.left.right = TreeNode(5)
33+
root.right.left = TreeNode(6)
34+
root.right.right = TreeNode(7)
35+
print("Inorder traversal of the tree:", in_order_traversal(root))
36+
print("Postorder traversal of the tree", post_order_traversal(root))

Listing_2_1_guess_number.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def guess_number(lower_bound, upper_bound, num):
2+
# expand the bounds by 1 to avoid getting stuck in infinite loops
3+
lower_bound -= 1
4+
upper_bound += 1
5+
num_guesses = 1
6+
guess = (lower_bound + upper_bound) // 2
7+
print (f"initial guess -->{guess}" )
8+
while guess != num:
9+
if guess > num:
10+
upper_bound = guess
11+
elif guess < num:
12+
lower_bound = guess
13+
else:
14+
print (f"Guessed it, the number is {guess}")
15+
guess = (lower_bound + upper_bound) // 2
16+
print (f"guess -->{guess}" )
17+
num_guesses += 1
18+
print (f"it takes {num_guesses} guesses to guess {num}")
19+
20+
if __name__ == "__main__":
21+
lower_bound = 1
22+
upper_bound = 100
23+
num = 99
24+
guess_number(lower_bound, upper_bound, num)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
def selection_sort(nums):
2+
def find_min_index(arr):
3+
min_idx = None
4+
min_val = float("inf")
5+
for idx, num in enumerate(arr):
6+
if num < min_val:
7+
min_idx, min_val = idx, num
8+
return min_idx, min_val
9+
10+
i = 0
11+
while i < len(nums):
12+
min_idx, min_val = find_min_index(nums[i:])
13+
if nums[i] > min_val:
14+
nums[i], nums[min_idx + i] = nums[min_idx + i], nums[i]
15+
i += 1
16+
return nums
17+
18+
def bubble_sort(nums):
19+
for i in range(len(nums)):
20+
for j in range(len(nums) - 1 - i):
21+
if nums[j] > nums[j+1]:
22+
nums[j], nums[j+1] = nums[j+1], nums[j]
23+
return nums
24+
25+
def insertion_sort(nums):
26+
for i in range(1, len(nums)):
27+
j = i
28+
while j > 0:
29+
if nums[j] < nums[j-1]:
30+
nums[j], nums[j-1] = nums[j-1], nums[j]
31+
j -= 1
32+
return nums
33+
34+
if __name__ == "__main__":
35+
unsorted_arr_1 = [5, -1, 0, 3, 2]
36+
sorted_arr_1 = selection_sort(unsorted_arr_1)
37+
print (sorted_arr_1)
38+
39+
unsorted_arr_2 = [5, -1, 0, 3, 2]
40+
bubble_sort(unsorted_arr_2)
41+
print (unsorted_arr_2)
42+
43+
unsorted_arr_3 = [5, -1, 0, 3, 2]
44+
insertion_sort(unsorted_arr_3)
45+
print (unsorted_arr_3)

0 commit comments

Comments
 (0)