Skip to content

Commit a85ea39

Browse files
author
Karandeep Grover
committedSep 29, 2020
moved to new repo
1 parent d452450 commit a85ea39

Some content is hidden

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

41 files changed

+4539
-0
lines changed
 

‎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))

‎2_Arrays/2_arrays_exercise.md

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/py/blob/master/DataStructures/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/py/blob/master/DataStructures/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/py/blob/master/DataStructures/2_Arrays/Solution/3_odd_even_numbers.py)
45+

0 commit comments

Comments
 (0)
Please sign in to comment.