├── .gitignore ├── Introduction └── Python Refresher │ ├── exercise6.py │ ├── exercise3.py │ ├── exercise1.py │ ├── exercise4.py │ ├── exercise2.py │ └── exercise5.py ├── projects ├── Show me the Data Structures │ ├── testdir │ │ ├── t1.c │ │ ├── t1.h │ │ ├── subdir1 │ │ │ ├── a.c │ │ │ └── a.h │ │ ├── subdir5 │ │ │ ├── a.c │ │ │ └── a.h │ │ ├── subdir2 │ │ │ └── .gitkeep │ │ ├── subdir4 │ │ │ └── .gitkeep │ │ └── subdir3 │ │ │ └── subsubdir1 │ │ │ ├── b.c │ │ │ └── b.h │ ├── explanation_1.pdf │ ├── explanation_2.pdf │ ├── explanation_3.pdf │ ├── explanation_4.pdf │ ├── explanation_5.pdf │ ├── explanation_6.pdf │ ├── problem_4.py │ ├── problem_2.py │ ├── problem_5.py │ ├── problem_6.py │ └── problem_1.py ├── Route Planner │ ├── assets │ │ └── a.png │ ├── map-10.pickle │ ├── map-40.pickle │ ├── test.py │ ├── LICENSE │ ├── README.md │ ├── helpers.py │ └── student_code.py ├── Problems vs. Algorithms │ ├── explanation_6.md │ ├── explanation_4.md │ ├── explanation_1.md │ ├── explanation_3.md │ ├── explanation_7.md │ ├── explanation_2.md │ ├── explanation_5.md │ ├── problem_6.py │ ├── problem_4.py │ ├── problem_1.py │ ├── problem_3.py │ ├── problem_2.py │ ├── problem_5.py │ └── problem_7.py └── Unscramble Computer Science Problems │ ├── Task1.py │ ├── Task0.py │ ├── Analysis.txt │ ├── Task4.py │ ├── Task2.py │ └── Task3.py ├── Data Structures ├── Trees │ ├── images │ │ ├── bst_01.png │ │ ├── tree_01.png │ │ └── tree_02.png │ └── resources │ │ └── 01-binary-tree.png ├── Recursion │ ├── Checking Palindrome.ipynb │ ├── Reversing a string.ipynb │ ├── Factorial using recursion.ipynb │ ├── Staircase.ipynb │ ├── Add-One-Again.ipynb │ ├── Last-index-recursion.ipynb │ └── Deep Reverse.ipynb ├── Stacks and Queues │ ├── python_stack_practice.ipynb │ ├── Build a Queue Using a Stack.ipynb │ ├── Build a Queue Using High-Level Python.ipynb │ ├── Balanced Parentheses Exercise - Stacks.ipynb │ └── Reverse a stack.ipynb ├── Maps and Hashing │ ├── Pair-Sum-to-target.ipynb │ └── String Key Hash Table.ipynb └── Arrays and Linked Lists │ ├── Max-Sum-Subarray.ipynb │ ├── Reverse a Linked List.ipynb │ ├── Duplicate-Number.ipynb │ ├── Pascal's-Triangle.ipynb │ └── Add-One.ipynb ├── assets └── data-structures-and-algorithms.jpg ├── leetcode_exercises ├── leet_code_75 │ └── level_1 │ │ ├── 1480_runnin_sum_1d_array.py │ │ └── 724_find_pivot_index.py └── 1094_car_pooling.py ├── hackerrank_exercises └── python_introduction │ └── gregorian_year.py ├── practice └── weird_game.py ├── LICENSE ├── README.md ├── Basic Algorithms └── Basic Algorithms │ ├── bubble_sort_exercises.ipynb │ ├── Sort-0-1-2.ipynb │ ├── Pair Sum Problem.ipynb │ ├── Case Specific Sorting of Strings.ipynb │ └── HeapSort.ipynb └── Advanced Algorithms ├── Greedy Algorithms └── Min_Operations.ipynb └── Graph Algorithms ├── graph_bfs.ipynb └── graph_dfs.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store -------------------------------------------------------------------------------- /Introduction/Python Refresher/exercise6.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/t1.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/t1.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir1/a.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir1/a.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir5/a.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir5/a.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir2/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir4/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir3/subsubdir1/b.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Show me the Data Structures/testdir/subdir3/subsubdir1/b.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /projects/Route Planner/assets/a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Route Planner/assets/a.png -------------------------------------------------------------------------------- /projects/Route Planner/map-10.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Route Planner/map-10.pickle -------------------------------------------------------------------------------- /projects/Route Planner/map-40.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Route Planner/map-40.pickle -------------------------------------------------------------------------------- /Data Structures/Trees/images/bst_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/Data Structures/Trees/images/bst_01.png -------------------------------------------------------------------------------- /Data Structures/Trees/images/tree_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/Data Structures/Trees/images/tree_01.png -------------------------------------------------------------------------------- /Data Structures/Trees/images/tree_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/Data Structures/Trees/images/tree_02.png -------------------------------------------------------------------------------- /assets/data-structures-and-algorithms.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/assets/data-structures-and-algorithms.jpg -------------------------------------------------------------------------------- /Data Structures/Trees/resources/01-binary-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/Data Structures/Trees/resources/01-binary-tree.png -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_1.pdf -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_2.pdf -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_3.pdf -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_4.pdf -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_5.pdf -------------------------------------------------------------------------------- /projects/Show me the Data Structures/explanation_6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmendozah/Data-Structures-and-Algorithms/HEAD/projects/Show me the Data Structures/explanation_6.pdf -------------------------------------------------------------------------------- /leetcode_exercises/leet_code_75/level_1/1480_runnin_sum_1d_array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | def runningSum(self, nums: List[int]) -> List[int]: 4 | for i in range(1, len(nums)): 5 | nums[i] += nums[i - 1] 6 | return nums 7 | 8 | 9 | print(runningSum(False,[1,2,3,4,5])) 10 | -------------------------------------------------------------------------------- /hackerrank_exercises/python_introduction/gregorian_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | 4 | # Write your logic here 5 | if (year % 4 == 0 and year % 100 != 0) or (year % 100 == 0 and year % 400 == 0): 6 | leap = True 7 | 8 | return leap 9 | 10 | 11 | print(is_leap(2100)) -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_6.md: -------------------------------------------------------------------------------- 1 | # Problem 6: Unsorted Integer Array 2 | 3 | To solve this problem I used a single while loop to iterate over the unsorted array of integers. 4 | 5 | ## Time Space Complexity 6 | 7 | **Time → O(n)**, because we iterate over the entire array once. 8 | 9 | **Space → O(1)**, because we just return the min and max numbers. -------------------------------------------------------------------------------- /leetcode_exercises/leet_code_75/level_1/724_find_pivot_index.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | def pivotIndex(self, nums: List[int]) -> int: 4 | S = sum(nums) 5 | left_sum = 0 6 | for i, x in enumerate(nums): 7 | if left_sum == (S - x - left_sum): 8 | return i 9 | left_sum += x 10 | return -1 11 | 12 | print(pivotIndex(False, [2,1,-1])) -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_4.md: -------------------------------------------------------------------------------- 1 | # Problem 4: Dutch National Flag Problem 2 | 3 | For this problem we do a single while loop adding O(n) time complexity. By switching the 0s and 2s because the 1s will automatically fall into their correct places. 4 | 5 | ## Time and Space complexity 6 | 7 | **Time → O(n)**, because we have a single while loop 8 | 9 | **Space → O(1)**, because we return the same array with it’s elements sorted -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_1.md: -------------------------------------------------------------------------------- 1 | # Problem 1: Square Root of an Integer 2 | 3 | To calculate the floored root of a number, it is clear the square root of that number will lie in the range [1, number]. But. Instead of checking each number on this range, the idea is to use binary search in order to efficiently find the floor square root of the number in O( log n) time complexity. 4 | 5 | ## Time Space Complexity 6 | 7 | **Time → O(log n)**, because we use binary search to search for the square root. 8 | 9 | **Space → O(1)**, we return a single value, the square floored root of the number. -------------------------------------------------------------------------------- /leetcode_exercises/1094_car_pooling.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def carPooling(self, trips: List[List[int]], capacity: int) -> bool: 6 | q = [] 7 | for n, start, end in trips: 8 | q.append((start, n)) 9 | q.append((end, -n)) 10 | total = 0 11 | for location, number in sorted(q): 12 | total += number 13 | if total > capacity: 14 | return False 15 | return True 16 | 17 | 18 | s = Solution() 19 | print(s.carPooling(trips=[[2, 1, 5], [3, 3, 7]], capacity=4) == False) 20 | -------------------------------------------------------------------------------- /Introduction/Python Refresher/exercise3.py: -------------------------------------------------------------------------------- 1 | def prod(a,b): 2 | # TODO change output to the product of a and b 3 | return a*b 4 | 5 | def fact_gen(): 6 | i = 1 7 | n = i 8 | while True: 9 | output = prod(n, i) 10 | yield output 11 | # TODO: update i and n 12 | # Hint: i is a successive integer and n is the previous product 13 | i += 1 14 | n = output 15 | 16 | 17 | # Test block 18 | my_gen = fact_gen() 19 | num = 5 20 | for i in range(num): 21 | print(next(my_gen)) 22 | 23 | # Correct result when num = 5: 24 | # 1 25 | # 2 26 | # 6 27 | # 24 28 | # 120 -------------------------------------------------------------------------------- /practice/weird_game.py: -------------------------------------------------------------------------------- 1 | 2 | def callPopints(ops) -> int: 3 | record = [] 4 | for c in ops: 5 | if c == "C": 6 | record = record[:-1] 7 | elif c == "D": 8 | previous = record[-1:] 9 | record.append(sum(previous) * 2) 10 | elif c == "+": 11 | last_t = record[-2:] 12 | record.append(sum(last_t)) 13 | else: 14 | num = int(c) 15 | record.append(num) 16 | 17 | return sum(record) 18 | 19 | 20 | t = '5 -2 4 C D 9 + +' 21 | result =callPopints(t.strip().split()) 22 | print(result) 23 | 24 | l = [1,2,3,4] 25 | print(l.pop()) 26 | print(l) -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_3.md: -------------------------------------------------------------------------------- 1 | # Problem 3: Rearrange Array Digits 2 | 3 | To solve this problem I implemented mergesort and reversed the process (from larger to smaller) this process takes O( n log n) time complexity and after sorting the array I implement two loops one to go through digits with even indices and the other to go through digits with odd indices both loops add O(n/2) + O(n/2) time complexity. 4 | We end up with O(n log n) + O(n/2) + O(n/2) + which can be just simplified as O(n log n). 5 | 6 | ## Space Time Complexity 7 | 8 | **Time → O(n log n)**, because we use merge sort. 9 | 10 | **Space → O(1)**, we just have number_1 and number_2 return values. -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_7.md: -------------------------------------------------------------------------------- 1 | # Problem 7: Request Routing in a Web Server with a Trie 2 | 3 | To solve this problem I did a Trie implementation and the solution got divided into ​insert​ and lookup​ parts. 4 | 5 | #Time Space Complexity 6 | 7 | **insert path** 8 | 9 | **Time → O(n)**, because we loop through a list of segments from a url path that has been split. 10 | 11 | **Space → O(n)**, because we store a segment in each node, depending on the number of segments once the url path has been split. 12 | 13 | **lookup path** 14 | 15 | **Time → O(n)**, because we loop through a list of segments from a url path that has been split. 16 | 17 | **Space → O(1)**, because we just return the path’s handler. 18 | -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_2.md: -------------------------------------------------------------------------------- 1 | # Problem 2: Search in a Rotated Sorted Array 2 | 3 | To solve the problem, First I look up for the pivot point, divide the array into two sub arrays and do binary search on both sub arrays. To find the pivot i call a recursive function which in the worst case adds O(n) time complexity. Second, we do binary search in the sub arrays so in the worst case the binary search adds O(log n) time complexity. 4 | We finally end up with O(n) + O(log n) which can be just simplified as O(log n). 5 | 6 | ## Time and Space complexity 7 | 8 | **Time → O (log n)**, because end up with O(n) + O(log n) which can be just simplified as O(log n). 9 | 10 | **Space → O(1)**, because we return a single value -------------------------------------------------------------------------------- /projects/Unscramble Computer Science Problems/Task1.py: -------------------------------------------------------------------------------- 1 | """ 2 | Read file into texts and calls. 3 | It's ok if you don't understand how to read files. 4 | """ 5 | import csv 6 | with open('texts.csv', 'r') as f: 7 | reader = csv.reader(f) 8 | texts = list(reader) 9 | 10 | with open('calls.csv', 'r') as f: 11 | reader = csv.reader(f) 12 | calls = list(reader) 13 | 14 | 15 | """ 16 | TASK 1: 17 | How many different telephone numbers are there in the records? 18 | Print a message: 19 | "There are different telephone numbers in the records." 20 | """ 21 | 22 | count = set() 23 | for text in texts: 24 | count.add(text[0]) 25 | count.add(text[1]) 26 | for call in calls: 27 | count.add(call[0]) 28 | count.add(call[1]) 29 | 30 | print(F"There are {len(count)} different telephone numbers in the records.") 31 | -------------------------------------------------------------------------------- /projects/Route Planner/test.py: -------------------------------------------------------------------------------- 1 | from helpers import load_map 2 | 3 | MAP_40_ANSWERS = [ 4 | (5, 34, [5, 16, 37, 12, 34]), 5 | (5, 5, [5]), 6 | (8, 24, [8, 14, 16, 37, 12, 17, 10, 24]) 7 | ] 8 | 9 | def test(shortest_path_function): 10 | map_40 = load_map('map-40.pickle') 11 | correct = 0 12 | for start, goal, answer_path in MAP_40_ANSWERS: 13 | path = shortest_path_function(map_40, start, goal) 14 | if path == answer_path: 15 | correct += 1 16 | else: 17 | print("For start:", start, 18 | "Goal: ", goal, 19 | "Your path:", path, 20 | "Correct: ", answer_path) 21 | if correct == len(MAP_40_ANSWERS): 22 | print("All tests pass! Congratulations!") 23 | else: 24 | print("You passed", correct, "/", len(MAP_40_ANSWERS), "test cases") 25 | -------------------------------------------------------------------------------- /Introduction/Python Refresher/exercise1.py: -------------------------------------------------------------------------------- 1 | def smallest_positive(in_list): 2 | # TODO: Define a control structure that finds the smallest positive 3 | # number in in_list and returns the correct smallest number. 4 | smallest_pos = None 5 | for num in in_list: 6 | if num > 0: 7 | # Note: we use a logical "or" in this solution to form 8 | # the conditional statement, although this was 9 | # not introduced above. 10 | if smallest_pos == None or num < smallest_pos: 11 | smallest_pos = num 12 | return smallest_pos 13 | 14 | # Test cases 15 | 16 | print(smallest_positive([4, -6, 7, 2, -4, 10])) 17 | # Correct output: 2 18 | 19 | print(smallest_positive([.2, 5, 3, -.1, 7, 7, 6])) 20 | # Correct output: 0.2 21 | 22 | print(smallest_positive([-6, -9, -7])) 23 | # Correct output: None 24 | 25 | print(smallest_positive([])) 26 | # Correct output: None -------------------------------------------------------------------------------- /projects/Problems vs. Algorithms/explanation_5.md: -------------------------------------------------------------------------------- 1 | # Problem 5: Autocomplete with Tries 2 | 3 | To solve this problem I implemented a Trie, the solution is divided into many parts: ​insert word​, 4 | find ​and ​finding​ ​suffixes. 5 | 6 | ## Time and Space Complexity 7 | 8 | **Insert word** 9 | 10 | **Time → O(n)**, because we loop through a list of characters and when we finish we set a flag to true. 11 | 12 | **Space → O(n)**, because we assign the characters in a word to multiple nodes. 13 | 14 | **Find** 15 | 16 | **Time → O(n)**, because we loop a word character per character. Space → O(1), because find returns a single node each time is called. 17 | 18 | **Find suffix** 19 | 20 | **Time → O(n)**, because it depends on the depth of the Trie. 21 | 22 | **Space → O(n x m)**, because the suffixes function runtime will expand with the number of inputs but also with the length of the inputs. Being n the number of inputs and m the average of the length of the word or input. -------------------------------------------------------------------------------- /projects/Unscramble Computer Science Problems/Task0.py: -------------------------------------------------------------------------------- 1 | """ 2 | Read file into texts and calls. 3 | It's ok if you don't understand how to read files. 4 | """ 5 | import csv 6 | with open('texts.csv', 'r') as f: 7 | reader = csv.reader(f) 8 | texts = list(reader) 9 | 10 | with open('calls.csv', 'r') as f: 11 | reader = csv.reader(f) 12 | calls = list(reader) 13 | 14 | 15 | """ 16 | TASK 0: 17 | What is the first record of texts and what is the last record of calls? 18 | Print messages: 19 | "First record of texts, texts at time