Skip to content

Commit f167a92

Browse files
author
codebasics
committed
2 parents 3174849 + 4d8a294 commit f167a92

21 files changed

+213
-32
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# Editor settings
132+
.vscode/settings.json

Diff for: algorithms/1_BinarySearch/binary_search_exercise.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
```
1212
This should return 5,6,7 as indices containing number 15 in the array
1313
14-
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/1_BinarySearch/binary_search_exercise_solution.py)
14+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/1_BinarySearch/binary_search_exercise_solution.py)

Diff for: algorithms/2_BubbleSort/bubble_sort_exercise.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Bubble Sort Exercise
22

3-
Modify [bubble_sort function](https://github.com/codebasics/py/blob/master/Algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
3+
Modify [bubble_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
44
```
55
elements = [
66
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
@@ -36,5 +36,5 @@ elements = [
3636
]
3737
```
3838

39-
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/2_BubbleSort/bubble_sort_exercise_solution.py)
39+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithmsAlgorithms/2_BubbleSort/bubble_sort_exercise_solution.py)
4040

Diff for: algorithms/3_QuickSort/quick_sort_exercise.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Implement quick sort using lumoto partition scheme. This partition scheme is explained in the video tutorial, you need to write python code to implement it.
33
Check the pseudo code here: https://en.wikipedia.org/wiki/Quicksort Check the section Lomuto partition scheme
44

5-
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/3_QuickSort/quick_sort_exercise_soluiton_lomuto.py)
5+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/3_QuickSort/quick_sort_exercise_soluiton_lomuto.py)
66

77

88

Diff for: algorithms/8_DepthFirstSearch/dfs.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# function for depth first search
2+
def dfs(data, start, visited=set()):
3+
4+
# if not visited print it
5+
if start not in visited:
6+
print(start, end=" ")
7+
8+
visited.add(start)
9+
10+
for i in data[start] - visited:
11+
12+
# if not visited gi in depth of it
13+
dfs(data, i, visited)
14+
return
15+
16+
17+
# sample data in dictionary form
18+
data = {
19+
'A': {'B'},
20+
'B': {'A', 'C', 'D'},
21+
'C': {'B', 'E'},
22+
'D': {'B', 'E'},
23+
'E': {'C', 'D', 'F'},
24+
'F': {'E'}
25+
}
26+
27+
28+
if __name__ == '__main__':
29+
30+
dfs(data, 'A')

Diff for: algorithms/8_DepthFirstSearch/dfs_exercise.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# function for depth first search
2+
def find_employees(data, start, employee, visited=set()):
3+
# if not visited print it
4+
if start not in visited:
5+
print(start, end=" ")
6+
if start == employee:
7+
print(":", end=" ")
8+
visited.add(start)
9+
10+
for i in data[start] - visited:
11+
# if not visited go in depth of it
12+
find_employees(data, i, visited)
13+
return
14+
15+
16+
# sample data in dictionary form
17+
data = {
18+
"karan": {"darshan", "nikhil"},
19+
"darshan": {"khantil", "tanuj"},
20+
"tanuj": {"nikhil"},
21+
"krinish": {"hetul"},
22+
"khantil": set(),
23+
"nikhil": set()
24+
}
25+
26+
27+
if __name__ == '__main__':
28+
29+
find_employees(data, "karan", "karan")

Diff for: algorithms/8_DepthFirstSearch/dfs_exerscise.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# DFS Exercise
2+
3+
![Overview : Employee hierarchy](https://github.com/beladiyadarshan/DFS/blob/main/emp.png?raw=true)
4+
5+
Given a graph containing managers and their employees as a dictionary of sets, print all employees reporting to a given manager.
6+
7+
```
8+
data = {
9+
"karan": {"darshan","nikhil"},
10+
"darshan": {"khantil", "tanuj"},
11+
"tanuj": {"nikhil"},
12+
"krinish": {"hetul"},
13+
"khantil" : set(),
14+
"nikhil" : set()
15+
}
16+
17+
18+
```
19+
20+
Example: Darshan and Nikhil are reporting to Karan. Khantil and Tanuj are reporting to Darshan.
21+
22+
```
23+
Q. Find all employees who are reporting to Karan.
24+
```
25+
26+
**Explanation:**
27+
28+
-So here, we want to find all the children nodes of Karan.
29+
30+
-We will perform DFS starting on Karan and then traverse all the children of Karan which are unvisited.
31+
32+
**Output:**
33+
34+
karan : nikhil darshan tanuj khantil
35+
36+
[Solution](https://github.com/beladiyadarshan/DFS/blob/main/dfs_exercise.py)

Diff for: algorithms/8_DepthFirstSearch/emp.png

10.1 KB
Loading

Diff for: algorithms/8_DepthFirstSearch/updated_graph.jpg

17.3 KB
Loading

Diff for: algorithms/9_BreadthFirstSearch/DFS_BFS_Graph.png

9.17 KB
Loading

Diff for: algorithms/9_BreadthFirstSearch/bfs.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def bfs(data, start, visited=set()):
2+
3+
queue = [start]
4+
5+
while queue:
6+
current_node = queue.pop(0)
7+
if current_node not in visited:
8+
print(current_node, end=" ")
9+
visited.add(current_node)
10+
11+
for i in data[current_node] - visited:
12+
queue.append(i)
13+
return
14+
15+
16+
if __name__ == '__main__':
17+
18+
data = {
19+
'A': {'B'}, 'B': {'A', 'C', 'D'}, 'C': {'B', 'E'}, 'D': {'B', 'E'},
20+
'E': {'C', 'D', 'F'}, 'F': {'E'}
21+
}
22+
23+
bfs(data, 'A')

Diff for: algorithms/9_BreadthFirstSearch/bfs_exercise.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# BFS_Traversal
2+
BFS Traversal of a graph in python using graph given in dictionary
3+
4+
Implement a function to find whether a path exists for a given set of airline routes. The routes are depicted using graphs as a dictionary of sets, where keys represent as source and elements of sets represent destinations. Print the path if it exists.
5+
6+
Example: A, B, C, D, E, F are the nodes of the graph.
7+
8+
9+
For example, if you are given following data:
10+
11+
```
12+
data = {
13+
'A': {'B'},
14+
'B': {'A', 'C', 'D'},
15+
'C': {'B', 'E'},
16+
'D': {'B', 'E'},
17+
'E': {'C', 'D', 'F'},
18+
'F': {'E'}
19+
}
20+
```
21+
22+
The resultant graph will be:
23+
24+
![alt text](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/DFS_BFS_Graph.png)
25+
26+
Example: Source is A, Destination is D.
27+
28+
Expected Output:
29+
30+
```
31+
Path : A->B->D
32+
```
33+
34+
[Solution](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/bfs_exercise_solution.py)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def bfs(data, start, end, visited=[]):
2+
queue = [start]
3+
4+
while queue:
5+
current_node = queue.pop(0)
6+
if current_node==end:
7+
print("Path: " + "->".join(visited) + "->" + end)
8+
return
9+
visited.append(current_node)
10+
11+
for i in data[current_node] - set(visited):
12+
queue.append(i)
13+
print("Path does not exist!")
14+
return
15+
16+
17+
if __name__ == '__main__':
18+
data = {
19+
'A': {'B'},
20+
'B': {'C', 'D'},
21+
'C': {'E'},
22+
'D': {'E'},
23+
'E': {'F'},
24+
'F': set()
25+
}
26+
bfs(data, 'A', 'D')

Diff for: data_structures/2_Arrays/2_arrays_exercise.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Create a list to store these monthly expenses and using that find out,
1717
got a refund of 200$. Make a correction to your monthly expense list
1818
based on this
1919

20-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/2_Arrays/Solution/1_expenses.py)
20+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/1_expenses.py)
2121

2222
2. You have a list of your favourite marvel super heros.
2323
```
@@ -35,11 +35,11 @@ Using this find out,
3535
Do that with one line of code.
3636
5. Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)
3737

38-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/2_Arrays/Solution/2_marvel.py)
38+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/2_marvel.py)
3939

4040

4141
3. Create a list of all odd numbers between 1 and a max number.
4242
Max number is something you need to take from a user using input() function
4343

44-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/2_Arrays/Solution/3_odd_even_numbers.py)
44+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/2_Arrays/Solution/3_odd_even_numbers.py)
4545

Diff for: data_structures/3_LinkedList/3_linked_list_exercise.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Exercise: Linked List
22

3-
1. In [LinkedList class](https://github.com/codebasics/py/blob/master/DataStructures/3_LinkedList/3_linked_list.py) that we implemented in our tutorial add following two methods,
3+
1. In [LinkedList class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/3_LinkedList/3_linked_list.py) that we implemented in our tutorial add following two methods,
44
```
55
def insert_after_value(self, data_after, data_to_insert):
66
# Search for first occurance of data_after value in linked list
@@ -26,7 +26,7 @@ Now make following calls,
2626
ll.remove_by_value("grapes")
2727
ll.print()
2828
```
29-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/3_LinkedList/Solution/singly_linked_list_exercise.py)
29+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py)
3030

3131
2. Implement doubly linked list. The only difference with regular linked list is that double linked has prev node reference as well. That way you can iterate in forward and backward direction.
3232
Your node class will look this this,
@@ -45,6 +45,6 @@ def print_forward(self):
4545
def print_backward(self):
4646
# Print linked list in reverse direction. Use node.prev for this.
4747
```
48-
Implement all other methods in [regular linked list class](https://github.com/codebasics/py/blob/master/DataStructures/3_LinkedList/3_linked_list.py) and make necessary changes for doubly linked list (you need to populate node.prev in all those methods)
48+
Implement all other methods in [regular linked list class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/3_LinkedList/3_linked_list.py) and make necessary changes for doubly linked list (you need to populate node.prev in all those methods)
4949

50-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/3_LinkedList/Solution/doubly_linked_list_exercise.py)
50+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/3_LinkedList/Solution/doubly_linked_list_exercise.py)
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# Exercise: Hash Table
22

3-
1. [nyc_weather.csv](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following,
3+
1. [nyc_weather.csv](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following,
44
1. What was the average temperature in first week of Jan
55
1. What was the maximum temperature in first 10 days of Jan
66

77
Figure out data structure that is best for this problem
88

9-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb)
9+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb)
1010

11-
2. [nyc_weather.csv](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following,
11+
2. [nyc_weather.csv](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following,
1212
1. What was the temperature on Jan 9?
1313
1. What was the temperature on Jan 4?
1414

1515
Figure out data structure that is best for this problem
1616

17-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb)
17+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb)
1818

19-
3. [poem.txt](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/poem.txt) Contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in python and print every word and its count as show below. Think about the best data structure that you can use to solve this problem and figure out why you selected that specific data structure.
19+
3. [poem.txt](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/poem.txt) Contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in python and print every word and its count as show below. Think about the best data structure that you can use to solve this problem and figure out why you selected that specific data structure.
2020
```
2121
'diverged': 2,
2222
'in': 3,
2323
'I': 8
2424
```
2525

26-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/exercise_poem_find_word_occurances.ipynb)
26+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/exercise_poem_find_word_occurances.ipynb)
2727

2828
4. Implement hash table where collisions are handled using linear probing. We learnt about linear probing in the video tutorial. Take the hash table implementation that uses chaining and modify methods to use **linear probing**. Keep MAX size of arr in hashtable as 10.
2929

30-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/exercise_hash_table_linear_probing.ipynb)
30+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/exercise_hash_table_linear_probing.ipynb)
3131

Diff for: data_structures/5_Stack/5_stack_exercise.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
## Data structure tutorial exercise: Stack
2-
1. Write a function in python that can reverse a string using stack data structure. Use [Stack class](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/5_stack.ipynb) from the tutorial.
2+
1. Write a function in python that can reverse a string using stack data structure. Use [Stack class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/5_stack.ipynb) from the tutorial.
33
```
44
reverse_string("We will conquere COVID-19") should return "91-DIVOC ereuqnoc lliw eW"
55
```
66
7-
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/Exercise/reverse_string.py)
7+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/Exercise/reverse_string.py)
88
9-
2. Write a function in python that checks if paranthesis in the string are balanced or not. Possible parantheses are "{}',"()" or "[]". Use [Stack class](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/5_stack.ipynb) from the tutorial.
9+
2. Write a function in python that checks if paranthesis in the string are balanced or not. Possible parantheses are "{}',"()" or "[]". Use [Stack class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/5_stack.ipynb) from the tutorial.
1010
```
1111
is_balanced("({a+b})") --> True
1212
is_balanced("))((a+b}{") --> False
@@ -15,4 +15,4 @@
1515
is_balanced("[a+b]*(x+2y)*{gg+kk}") --> True
1616
```
1717
18-
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/Exercise/balance_paran.py)
18+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/Exercise/balance_paran.py)

Diff for: data_structures/6_Queue/6_queue_exercise.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Data structure tutorial exercise: Queue
22

3-
For all exercises use [Queue class](https://github.com/codebasics/py/blob/master/DataStructures/6_Queue/6_queue.ipynb) implemented in main tutorial.
3+
For all exercises use [Queue class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/6_queue.ipynb) implemented in main tutorial.
44

55
1. Design a food ordering system where your python program will run two threads,
66
1. Place Order: This thread will be placing an order and inserting that into a queue. This thread places new order every 0.5 second. (hint: use time.sleep(0.5) function)
@@ -15,7 +15,7 @@ For all exercises use [Queue class](https://github.com/codebasics/py/blob/master
1515
This problem is a producer,consumer problem where place_order thread is producing orders whereas server_order thread is consuming the food orders.
1616
Use Queue class implemented in a video tutorial.
1717
18-
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/6_Queue/Exercise/food_ordering_system.py)
18+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/Exercise/food_ordering_system.py)
1919
2020
2. Write a program to print binary numbers from 1 to 10 using Queue. Use Queue class implemented in main tutorial.
2121
Binary sequence should look like,
@@ -35,4 +35,4 @@ Hint: Notice a pattern above. After 1 second and third number is 1+0 and 1+1. 4t
3535
3636
You also need to add front() function in queue class that can return the front element in the queue.
3737
38-
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/6_Queue/Exercise/binary_numbers.py)
38+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/Exercise/binary_numbers.py)

Diff for: data_structures/7_Tree/7_tree_exercise.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
![ss](management_both.PNG)
66

7-
Extent [tree class](https://github.com/codebasics/py/blob/master/DataStructures/7_Tree/7_tree.py) built in our
7+
Extent [tree class](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/7_Tree/7_tree.py) built in our
88
main tutorial so that it takes **name** and **designation** in data part of TreeNode class.
99
Now extend print_tree function such that it can print either name tree, designation tree or name and designation tree. As shown below,
1010

@@ -19,7 +19,7 @@ if __name__ == '__main__':
1919
root_node.print_tree("both") # prints both (name and designation) hierarchy
2020
```
2121

22-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/7_Tree/Exercise/management_hierarchy.py)
22+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/7_Tree/Exercise/management_hierarchy.py)
2323

2424
2. Build below location tree using **TreeNode** class
2525

@@ -29,6 +29,6 @@ Now modify print_tree method to take tree level as input. And that should print
2929

3030
![](location_trees_all.png)
3131

32-
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/7_Tree/Exercise/location_hierarchy.py)
32+
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/7_Tree/Exercise/location_hierarchy.py)
3333

3434

0 commit comments

Comments
 (0)