Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
codebasics committed Feb 10, 2021
2 parents 3174849 + 4d8a294 commit f167a92
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Editor settings
.vscode/settings.json
2 changes: 1 addition & 1 deletion algorithms/1_BinarySearch/binary_search_exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
```
This should return 5,6,7 as indices containing number 15 in the array
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/1_BinarySearch/binary_search_exercise_solution.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/1_BinarySearch/binary_search_exercise_solution.py)
4 changes: 2 additions & 2 deletions algorithms/2_BubbleSort/bubble_sort_exercise.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Bubble Sort Exercise

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,
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,
```
elements = [
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
Expand Down Expand Up @@ -36,5 +36,5 @@ elements = [
]
```

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

2 changes: 1 addition & 1 deletion algorithms/3_QuickSort/quick_sort_exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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.
Check the pseudo code here: https://en.wikipedia.org/wiki/Quicksort Check the section Lomuto partition scheme

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



30 changes: 30 additions & 0 deletions algorithms/8_DepthFirstSearch/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# function for depth first search
def dfs(data, start, visited=set()):

# if not visited print it
if start not in visited:
print(start, end=" ")

visited.add(start)

for i in data[start] - visited:

# if not visited gi in depth of it
dfs(data, i, visited)
return


# sample data in dictionary form
data = {
'A': {'B'},
'B': {'A', 'C', 'D'},
'C': {'B', 'E'},
'D': {'B', 'E'},
'E': {'C', 'D', 'F'},
'F': {'E'}
}


if __name__ == '__main__':

dfs(data, 'A')
29 changes: 29 additions & 0 deletions algorithms/8_DepthFirstSearch/dfs_exercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# function for depth first search
def find_employees(data, start, employee, visited=set()):
# if not visited print it
if start not in visited:
print(start, end=" ")
if start == employee:
print(":", end=" ")
visited.add(start)

for i in data[start] - visited:
# if not visited go in depth of it
find_employees(data, i, visited)
return


# sample data in dictionary form
data = {
"karan": {"darshan", "nikhil"},
"darshan": {"khantil", "tanuj"},
"tanuj": {"nikhil"},
"krinish": {"hetul"},
"khantil": set(),
"nikhil": set()
}


if __name__ == '__main__':

find_employees(data, "karan", "karan")
36 changes: 36 additions & 0 deletions algorithms/8_DepthFirstSearch/dfs_exerscise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# DFS Exercise

![Overview : Employee hierarchy](https://github.com/beladiyadarshan/DFS/blob/main/emp.png?raw=true)

Given a graph containing managers and their employees as a dictionary of sets, print all employees reporting to a given manager.

```
data = {
"karan": {"darshan","nikhil"},
"darshan": {"khantil", "tanuj"},
"tanuj": {"nikhil"},
"krinish": {"hetul"},
"khantil" : set(),
"nikhil" : set()
}
```

Example: Darshan and Nikhil are reporting to Karan. Khantil and Tanuj are reporting to Darshan.

```
Q. Find all employees who are reporting to Karan.
```

**Explanation:**

-So here, we want to find all the children nodes of Karan.

-We will perform DFS starting on Karan and then traverse all the children of Karan which are unvisited.

**Output:**

karan : nikhil darshan tanuj khantil

[Solution](https://github.com/beladiyadarshan/DFS/blob/main/dfs_exercise.py)
Binary file added algorithms/8_DepthFirstSearch/emp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithms/8_DepthFirstSearch/updated_graph.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithms/9_BreadthFirstSearch/DFS_BFS_Graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions algorithms/9_BreadthFirstSearch/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def bfs(data, start, visited=set()):

queue = [start]

while queue:
current_node = queue.pop(0)
if current_node not in visited:
print(current_node, end=" ")
visited.add(current_node)

for i in data[current_node] - visited:
queue.append(i)
return


if __name__ == '__main__':

data = {
'A': {'B'}, 'B': {'A', 'C', 'D'}, 'C': {'B', 'E'}, 'D': {'B', 'E'},
'E': {'C', 'D', 'F'}, 'F': {'E'}
}

bfs(data, 'A')
34 changes: 34 additions & 0 deletions algorithms/9_BreadthFirstSearch/bfs_exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# BFS_Traversal
BFS Traversal of a graph in python using graph given in dictionary

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.

Example: A, B, C, D, E, F are the nodes of the graph.


For example, if you are given following data:

```
data = {
'A': {'B'},
'B': {'A', 'C', 'D'},
'C': {'B', 'E'},
'D': {'B', 'E'},
'E': {'C', 'D', 'F'},
'F': {'E'}
}
```

The resultant graph will be:

![alt text](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/DFS_BFS_Graph.png)

Example: Source is A, Destination is D.

Expected Output:

```
Path : A->B->D
```

[Solution](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/bfs_exercise_solution.py)
26 changes: 26 additions & 0 deletions algorithms/9_BreadthFirstSearch/bfs_exercise_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def bfs(data, start, end, visited=[]):
queue = [start]

while queue:
current_node = queue.pop(0)
if current_node==end:
print("Path: " + "->".join(visited) + "->" + end)
return
visited.append(current_node)

for i in data[current_node] - set(visited):
queue.append(i)
print("Path does not exist!")
return


if __name__ == '__main__':
data = {
'A': {'B'},
'B': {'C', 'D'},
'C': {'E'},
'D': {'E'},
'E': {'F'},
'F': set()
}
bfs(data, 'A', 'D')
6 changes: 3 additions & 3 deletions data_structures/2_Arrays/2_arrays_exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Create a list to store these monthly expenses and using that find out,
got a refund of 200$. Make a correction to your monthly expense list
based on this

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

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

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


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

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

8 changes: 4 additions & 4 deletions data_structures/3_LinkedList/3_linked_list_exercise.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Exercise: Linked List

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,
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,
```
def insert_after_value(self, data_after, data_to_insert):
# Search for first occurance of data_after value in linked list
Expand All @@ -26,7 +26,7 @@ Now make following calls,
ll.remove_by_value("grapes")
ll.print()
```
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/3_LinkedList/Solution/singly_linked_list_exercise.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py)

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.
Your node class will look this this,
Expand All @@ -45,6 +45,6 @@ def print_forward(self):
def print_backward(self):
# Print linked list in reverse direction. Use node.prev for this.
```
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)
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)

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

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,
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,
1. What was the average temperature in first week of Jan
1. What was the maximum temperature in first 10 days of Jan

Figure out data structure that is best for this problem

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

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,
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,
1. What was the temperature on Jan 9?
1. What was the temperature on Jan 4?

Figure out data structure that is best for this problem

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

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.
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.
```
'diverged': 2,
'in': 3,
'I': 8
```

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

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.

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

8 changes: 4 additions & 4 deletions data_structures/5_Stack/5_stack_exercise.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Data structure tutorial exercise: Stack
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.
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.
```
reverse_string("We will conquere COVID-19") should return "91-DIVOC ereuqnoc lliw eW"
```
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/Exercise/reverse_string.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/Exercise/reverse_string.py)
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.
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.
```
is_balanced("({a+b})") --> True
is_balanced("))((a+b}{") --> False
Expand All @@ -15,4 +15,4 @@
is_balanced("[a+b]*(x+2y)*{gg+kk}") --> True
```
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/5_Stack/Exercise/balance_paran.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/5_Stack/Exercise/balance_paran.py)
6 changes: 3 additions & 3 deletions data_structures/6_Queue/6_queue_exercise.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Data structure tutorial exercise: Queue

For all exercises use [Queue class](https://github.com/codebasics/py/blob/master/DataStructures/6_Queue/6_queue.ipynb) implemented in main tutorial.
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.

1. Design a food ordering system where your python program will run two threads,
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)
Expand All @@ -15,7 +15,7 @@ For all exercises use [Queue class](https://github.com/codebasics/py/blob/master
This problem is a producer,consumer problem where place_order thread is producing orders whereas server_order thread is consuming the food orders.
Use Queue class implemented in a video tutorial.
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/6_Queue/Exercise/food_ordering_system.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/Exercise/food_ordering_system.py)
2. Write a program to print binary numbers from 1 to 10 using Queue. Use Queue class implemented in main tutorial.
Binary sequence should look like,
Expand All @@ -35,4 +35,4 @@ Hint: Notice a pattern above. After 1 second and third number is 1+0 and 1+1. 4t
You also need to add front() function in queue class that can return the front element in the queue.
[Solution](https://github.com/codebasics/py/tree/master/DataStructures/6_Queue/Exercise/binary_numbers.py)
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/Exercise/binary_numbers.py)
6 changes: 3 additions & 3 deletions data_structures/7_Tree/7_tree_exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![ss](management_both.PNG)

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

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

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

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

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

![](location_trees_all.png)

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


Loading

0 comments on commit f167a92

Please sign in to comment.