Skip to content

Commit 6e2d46a

Browse files
Updated
1 parent c4cba83 commit 6e2d46a

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

Chapter03/Fibonacci_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def fib(n):
2+
if n <= 1:
3+
return 1
4+
else:
5+
return fib(n-1) + fib(n-2)
6+
7+
for i in range(5):
8+
print(fib(i))

Chapter03/binary_search.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def binary_search(arr, start, end, key):
2+
while start <= end:
3+
mid = start + (end - start)/2)
4+
if arr[mid] == key:
5+
return mid
6+
elif arr[mid] < key:
7+
start = mid + 1
8+
else:
9+
end = mid - 1
10+
return -1
11+
12+
13+
arr = [ 4, 6, 9, 13, 14, 18, 21, 24, 38]
14+
x = 13
15+
result = binary_search(arr, 0, len(arr)-1, x)
16+
print(result)
17+
18+

Chapter03/dijkstra.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
def get_shortest_distance(table, vertex):
3+
shortest_distance = table[vertex][DISTANCE]
4+
return shortest_distance
5+
6+
def set_shortest_distance(table, vertex, new_distance):
7+
table[vertex][DISTANCE] = new_distance
8+
9+
def set_previous_node(table, vertex, previous_node):
10+
table[vertex][PREVIOUS_NODE] = previous_node
11+
12+
def get_distance(graph, first_vertex, second_vertex):
13+
return graph[first_vertex][second_vertex]
14+
15+
def get_next_node(table, visited_nodes):
16+
unvisited_nodes = list(set(table.keys()).difference(set(visited_nodes)))
17+
assumed_min = table[unvisited_nodes[0]][DISTANCE]
18+
min_vertex = unvisited_nodes[0]
19+
for node in unvisited_nodes:
20+
if table[node][DISTANCE] < assumed_min:
21+
assumed_min = table[node][DISTANCE]
22+
min_vertex = node
23+
return min_vertex
24+
25+
def find_shortest_path(graph, table, origin):
26+
visited_nodes = []
27+
current_node = origin
28+
starting_node = origin
29+
while True:
30+
adjacent_nodes = graph[current_node]
31+
if set(adjacent_nodes).issubset(set(visited_nodes)):
32+
# Nothing here to do. All adjacent nodes have been visited.
33+
pass
34+
else:
35+
unvisited_nodes = set(adjacent_nodes).difference(set(visited_nodes))
36+
for vertex in unvisited_nodes:
37+
distance_from_starting_node = get_shortest_distance(table, vertex)
38+
if distance_from_starting_node == INFINITY and current_node == starting_node:
39+
total_distance = get_distance(graph, vertex, current_node)
40+
else:
41+
total_distance = get_shortest_distance (table,
42+
current_node) + get_distance(graph, current_node, vertex)
43+
if total_distance < distance_from_starting_node:
44+
set_shortest_distance(table, vertex, total_distance)
45+
set_previous_node(table, vertex, current_node)
46+
visited_nodes.append(current_node)
47+
#print(visited_nodes)
48+
if len(visited_nodes) == len(table.keys()):
49+
break
50+
current_node = get_next_node(table,visited_nodes)
51+
return(table)
52+
53+
graph = dict()
54+
graph['A'] = {'B': 5, 'D': 9, 'E': 2}
55+
graph['B'] = {'A': 5, 'C': 2}
56+
graph['C'] = {'B': 2, 'D': 3}
57+
graph['D'] = {'A': 9, 'F': 2, 'C': 3}
58+
graph['E'] = {'A': 2, 'F': 3}
59+
graph['F'] = {'E': 3, 'D': 2}
60+
61+
table = dict()
62+
table = {
63+
'A': [0, None],
64+
'B': [float("inf"), None],
65+
'C': [float("inf"), None],
66+
'D': [float("inf"), None],
67+
'E': [float("inf"), None],
68+
'F': [float("inf"), None],
69+
}
70+
71+
72+
DISTANCE = 0
73+
PREVIOUS_NODE = 1
74+
INFINITY = float('inf')
75+
76+
shortest_distance_table = find_shortest_path(graph, table, 'A')
77+
78+
79+
for k in sorted(shortest_distance_table):
80+
print("{} - {}".format(k,shortest_distance_table[k]))

Chapter03/dyna_fib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def dyna_fib(n):
2+
if n == 0:
3+
return 0
4+
if n == 1:
5+
return 1
6+
if lookup[n] is not None:
7+
return lookup[n]
8+
9+
lookup[n] = dyna_fib(n-1) + dyna_fib(n-2)
10+
return lookup[n]
11+
12+
13+
lookup = [None]*(1000)
14+
15+
16+
for i in range(6):
17+
print(dyna_fib(i))

Chapter03/factorial.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def factorial(n):
2+
# test for a base case
3+
if n == 0:
4+
return 1
5+
else:
6+
return n*factorial(n-1) # Do the calculation and a recursive call
7+
8+
9+
10+
print(factorial(4))

Chapter03/merge_sort.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def merge_sort(unsorted_list):
2+
if len(unsorted_list) == 1:
3+
return unsorted_list
4+
mid_point = int(len(unsorted_list)/2)
5+
first_half = unsorted_list[:mid_point]
6+
second_half = unsorted_list[mid_point:]
7+
8+
half_a = merge_sort(first_half)
9+
half_b = merge_sort(second_half)
10+
11+
return merge(half_a, half_b)
12+
13+
14+
15+
def merge(first_sublist, second_sublist):
16+
i = j = 0
17+
merged_list = []
18+
while i < len(first_sublist) and j < len(second_sublist):
19+
if first_sublist[i] < second_sublist[j]:
20+
merged_list.append(first_sublist[i])
21+
i += 1
22+
else:
23+
merged_list.append(second_sublist[j])
24+
j += 1
25+
while i < len(first_sublist):
26+
merged_list.append(first_sublist[i])
27+
i += 1
28+
while j < len(second_sublist):
29+
merged_list.append(second_sublist[j])
30+
j += 1
31+
return merged_list
32+
33+
34+
a= [11, 12, 7, 41, 61, 13, 16, 14]
35+
print(merge_sort(a))

0 commit comments

Comments
 (0)