Skip to content

Commit 8f90358

Browse files
Completed all Data Structure Exercises
1 parent 7d353b8 commit 8f90358

32 files changed

+3307
-0
lines changed

Data_Structures/10_Graph/graph.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class Graph:
2+
def __init__(self, edges):
3+
self.edges = edges
4+
self.graph_dict = {}
5+
for start, end in edges:
6+
if start in self.graph_dict:
7+
self.graph_dict[start].append(end)
8+
else:
9+
self.graph_dict[start] = [end]
10+
print("Graph Dict:", self.graph_dict)
11+
12+
def get_paths(self, start, end, path=[]):
13+
path = path + [start]
14+
15+
if start == end:
16+
return [path]
17+
18+
if start not in self.graph_dict:
19+
return []
20+
21+
paths = []
22+
for node in self.graph_dict[start]:
23+
if node not in path:
24+
new_paths = self.get_paths(node, end, path)
25+
for p in new_paths:
26+
paths.append(p)
27+
28+
return paths
29+
30+
def get_shortest_path(self, start, end, path=[]):
31+
path = path + [start]
32+
33+
if start == end:
34+
return path
35+
36+
if start not in self.graph_dict:
37+
return None
38+
39+
shortest_path = None
40+
for node in self.graph_dict[start]:
41+
if node not in path:
42+
sp = self.get_shortest_path(node, end, path)
43+
if sp:
44+
if shortest_path is None or len(sp) < len(shortest_path):
45+
shortest_path = sp
46+
47+
return shortest_path
48+
49+
if __name__ == '__main__':
50+
51+
routes = [
52+
("Mumbai","Pune"),
53+
("Mumbai", "Surat"),
54+
("Surat", "Bangaluru"),
55+
("Pune","Hyderabad"),
56+
("Pune","Mysuru"),
57+
("Hyderabad","Bangaluru"),
58+
("Hyderabad", "Chennai"),
59+
("Mysuru", "Bangaluru"),
60+
("Chennai", "Bangaluru")
61+
]
62+
63+
routes = [
64+
("Mumbai", "Paris"),
65+
("Mumbai", "Dubai"),
66+
("Paris", "Dubai"),
67+
("Paris", "New York"),
68+
("Dubai", "New York"),
69+
("New York", "Toronto"),
70+
]
71+
72+
route_graph = Graph(routes)
73+
74+
start = "Mumbai"
75+
end = "New York"
76+
77+
print(f"All paths between: {start} and {end}: ",route_graph.get_paths(start,end))
78+
print(f"Shortest path between {start} and {end}: ", route_graph.get_shortest_path(start,end))
79+
80+
start = "Dubai"
81+
end = "New York"
82+
83+
print(f"All paths between: {start} and {end}: ",route_graph.get_paths(start,end))
84+
print(f"Shortest path between {start} and {end}: ", route_graph.get_shortest_path(start,end))

Data_Structures/2_Arrays/2-1.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# # Exercise: Array DataStructure
2+
3+
# 1. Let us say your expense for every month are listed below,
4+
# 1. January - 2200
5+
# 2. February - 2350
6+
# 3. March - 2600
7+
# 4. April - 2130
8+
# 5. May - 2190
9+
10+
# Create a list to store these monthly expenses and using that find out,
11+
12+
# 1. In Feb, how many dollars you spent extra compare to January?
13+
# 2. Find out your total expense in first quarter (first three months) of the year.
14+
# 3. Find out if you spent exactly 2000 dollars in any month
15+
# 4. June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
16+
# 5. You returned an item that you bought in a month of April and
17+
# got a refund of 200$. Make a correction to your monthly expense list
18+
# based on this
19+
20+
expenses = [2200,2350,2600,2130,2190]
21+
22+
# 1. In Feb, how many dollars you spent extra compare to January?
23+
24+
extraSpend = expenses[1] - expenses[0]
25+
26+
print(f"In February, you spent ${extraSpend} more than in January")
27+
28+
# 2. Find out your total expense in first quarter (first three months) of the year.
29+
30+
q1_expenses = sum(expenses[0:3])
31+
32+
print(f"In Q1, you spent ${q1_expenses}")
33+
34+
# 3. Find out if you spent exactly 2000 dollars in any month
35+
36+
print("Did I spent 2000$ in any month? ", 2000 in expenses) # False
37+
38+
did_i_spend = False
39+
40+
for e in expenses:
41+
if e == 2000:
42+
did_i_spend = True
43+
44+
if did_i_spend == True:
45+
print('You did spend exactly 2000 in a month')
46+
else:
47+
print('You did not spend exactly 2000 in a month')
48+
49+
#4. June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
50+
51+
expenses.append(1980)
52+
53+
print(expenses)
54+
55+
# 5. You returned an item that you bought in a month of April and
56+
# # got a refund of 200$. Make a correction to your monthly expense list
57+
# # based on this
58+
59+
expenses[3] = expenses[3] - 200
60+
print(expenses)

Data_Structures/2_Arrays/2-2.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 2. You have a list of your favourite marvel super heros.
2+
# ```
3+
# heros=['spider man','thor','hulk','iron man','captain america']
4+
# ```
5+
6+
# Using this find out,
7+
8+
# 1. Length of the list
9+
# 2. Add 'black panther' at the end of this list
10+
# 3. You realize that you need to add 'black panther' after 'hulk',
11+
# so remove it from the list first and then add it after 'hulk'
12+
# 4. Now you don't like thor and hulk because they get angry easily :)
13+
# So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool).
14+
# Do that with one line of code.
15+
# 5. Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)
16+
17+
18+
heros=['spider man','thor','hulk','iron man','captain america']
19+
20+
# 1. Length of the list
21+
print("length of heros list:",len(heros))
22+
23+
# 2. Add 'black panther' at the end of this list
24+
25+
heros.append('black panther')
26+
print(heros)
27+
28+
# 3. You realize that you need to add 'black panther' after 'hulk',
29+
# so remove it from the list first and then add it after 'hulk'
30+
31+
heros.remove('black panther')
32+
heros.insert(3,'black panther')
33+
print(heros)
34+
35+
# 4. Now you don't like thor and hulk because they get angry easily :)
36+
# So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool).
37+
# Do that with one line of code.
38+
39+
heros[1:3] = ['doctor stranger']
40+
print(heros)
41+
42+
# 5. Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)
43+
44+
heros.sort()
45+
print(heros)
46+
47+

Data_Structures/2_Arrays/2-3.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 3. Create a list of all odd numbers between 1 and a max number.
2+
# Max number is something you need to take from a user using input() function
3+
4+
odd_numbers_list = []
5+
6+
max_number = int(input('What is the max number? '))
7+
8+
for i in range(0,max_number+1):
9+
if i%2 != 0:
10+
odd_numbers_list.append(i)
11+
12+
print(odd_numbers_list)
13+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Exercise: Array DataStructure
2+
3+
1. Let us say your expense for every month are listed below,
4+
1. January - 2200
5+
2. February - 2350
6+
3. March - 2600
7+
4. April - 2130
8+
5. May - 2190
9+
10+
Create a list to store these monthly expenses and using that find out,
11+
12+
1. In Feb, how many dollars you spent extra compare to January?
13+
2. Find out your total expense in first quarter (first three months) of the year.
14+
3. Find out if you spent exactly 2000 dollars in any month
15+
4. June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
16+
5. You returned an item that you bought in a month of April and
17+
got a refund of 200$. Make a correction to your monthly expense list
18+
based on this
19+
20+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/1_expenses.py)
21+
22+
2. You have a list of your favourite marvel super heros.
23+
```
24+
heros=['spider man','thor','hulk','iron man','captain america']
25+
```
26+
27+
Using this find out,
28+
29+
1. Length of the list
30+
2. Add 'black panther' at the end of this list
31+
3. You realize that you need to add 'black panther' after 'hulk',
32+
so remove it from the list first and then add it after 'hulk'
33+
4. Now you don't like thor and hulk because they get angry easily :)
34+
So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool).
35+
Do that with one line of code.
36+
5. Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)
37+
38+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/2_marvel.py)
39+
40+
41+
3. Create a list of all odd numbers between 1 and a max number.
42+
Max number is something you need to take from a user using input() function
43+
44+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/3_odd_even_numbers.py)
45+

0 commit comments

Comments
 (0)