diff --git a/Arrays/README.md b/Arrays/README.md new file mode 100644 index 0000000..864e65e --- /dev/null +++ b/Arrays/README.md @@ -0,0 +1,21 @@ +# Arrays + +This directory contains problems and solutions related to array data structures. + +## Contents + +### Anagram Check +Checks if two strings are anagrams of each other. +- [Anagram_Check_Sorted_Sol.py](Anagram_Check_Sorted_Sol.py) +- [Anagram_Check_manual_Sol.py](Anagram_Check_manual_Sol.py) + +### Array Pair Sum +Given an integer array, output all unique pairs that sum up to a specific value `k`. +- [ArrayPairSumSol.py](ArrayPairSumSol.py) + +### Find Missing Element +Find the missing element in a shuffled second array. +- [ArrayFindTheMissingElement_XOR_sol.py](ArrayFindTheMissingElement_XOR_sol.py) +- [ArrayFindTheMissingElement_brute_force_sol.py](ArrayFindTheMissingElement_brute_force_sol.py) +- [ArrayFindTheMissingElement_hash_table_sol.py](ArrayFindTheMissingElement_hash_table_sol.py) +- [ArrayFindTheMissingElement_takingSumandSubtract_sol.py](ArrayFindTheMissingElement_takingSumandSubtract_sol.py) diff --git a/Error-debug/README.md b/Error-debug/README.md new file mode 100644 index 0000000..b2a0155 --- /dev/null +++ b/Error-debug/README.md @@ -0,0 +1,9 @@ +# Error and Debugging + +This directory contains examples of error handling and debugging in Python. + +## Contents + +### Basic Exception Handling +Demonstrates `try-except-else-finally` blocks and user input validation. +- [ErrorExceptions.py](ErrorExceptions.py) diff --git a/GraphAlgorithms/README.md b/GraphAlgorithms/README.md new file mode 100644 index 0000000..8d10882 --- /dev/null +++ b/GraphAlgorithms/README.md @@ -0,0 +1,18 @@ +# Graph Algorithms + +This directory contains implementations of various graph-based algorithms. + +## Contents + +### Adjacency List Implementation +Basic implementation of a graph using an adjacency list. +- [AdjacencyListGraphImple.py](AdjacencyListGraphImple.py) + +### Breadth First Search (BFS) +- [BFS.py](BFS.py) +- [WordLadderProblem.py](WordLadderProblem.py) (Implementation using BFS) + +### Depth First Search (DFS) +- [DFSGeneral.py](DFSGeneral.py) +- [DFSImpleTheKnightsTourProblem.py](DFSImpleTheKnightsTourProblem.py) +- [TheKnightsTourProblem.py](TheKnightsTourProblem.py) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cecd15e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Pradeep K. Pant + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/LinkedLists/README.md b/LinkedLists/README.md new file mode 100644 index 0000000..5221b36 --- /dev/null +++ b/LinkedLists/README.md @@ -0,0 +1,23 @@ +# Linked Lists + +This directory contains implementations and common problems related to linked lists. + +## Contents + +### Singly Linked List Implementation +- [SingleLinkedListImple.py](SingleLinkedListImple.py) + +### Doubly Linked List Implementation +- [DoublyLinkedListImple.py](DoublyLinkedListImple.py) + +### Singly Linked List Cycle Check +Check if a singly linked list contains a cycle. +- [SinglyLinkedListCycleCheckImple.py](SinglyLinkedListCycleCheckImple.py) + +### Reverse a Linked List +Reverse a linked list in-place. +- [LinkedListReversal.py](LinkedListReversal.py) + +### Nth to Last Node +Find the nth to last node in a linked list. +- [LinkedListNthToLastNode.py](LinkedListNthToLastNode.py) diff --git a/Queues/README.md b/Queues/README.md new file mode 100644 index 0000000..8541c6b --- /dev/null +++ b/Queues/README.md @@ -0,0 +1,13 @@ +# Queues + +This directory contains implementations and problems related to queue data structures. + +## Contents + +### Queue Implementation +Basic queue operations (FIFO): `enqueue`, `dequeue`, `isEmpty`, `size`. +- [QueueImple.py](QueueImple.py) + +### Queue with Two Stacks +Implement a queue using two stacks. +- [QueueWith2StacksImple.py](QueueWith2StacksImple.py) diff --git a/README.md b/README.md index 790acf4..4a5b539 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ This repository contains a collection of common programming problems and data structure implementations in Python. ## Table of Contents +- [Usage](#usage) - [Arrays](#arrays) - [Linked Lists](#linked-lists) - [Stacks](#stacks) @@ -14,6 +15,18 @@ This repository contains a collection of common programming problems and data st - [Graph Algorithms](#graph-algorithms) - [Error Handling](#error-handling) - [Acknowledgments](#acknowledgments) +- [License](#license) + +--- + +## Usage + +Most scripts in this repository are standalone and can be executed directly using the Python 3 interpreter. + +Example: +```bash +python3 Arrays/Anagram_Check_Sorted_Sol.py +``` --- @@ -208,3 +221,8 @@ Demonstrates `try-except-else-finally` blocks and user input validation. - [Python for Data Structures, Algorithms, and Interviews on Udemy](https://www.udemy.com) - [Problem Solving with Algorithms and Data Structures using Python](http://interactivepython.org/runestone/static/pythonds/index.html) - [Big O Notation - Plain English Explanation](https://stackoverflow.com/questions/487258/what-is-a-plain-english-explanation-of-big-o-notation/487278#487278) + +--- + +## License +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/Recursion/FibonacciSeqDynamic.py b/Recursion/FibonacciSeqDynamic.py index 7bca04c..faa81b2 100644 --- a/Recursion/FibonacciSeqDynamic.py +++ b/Recursion/FibonacciSeqDynamic.py @@ -2,25 +2,26 @@ # memoization + recursion = dynamic programming # Author: Pradeep K. Pant, ppant@cpan.org - # Instantiate Cache information - n = 10 - cache = [None] * (n + 1) +# Instantiate Cache information +n = 10 +cache = [None] * (n + 1) # Implement fibonacci_dynamic - def fibonacci_dynamic(n): - # Base Case - if n == 0 or n == 1: - return n +def fibonacci_dynamic(n): + # Base Case + if n == 0 or n == 1: + return n - # Check cache if already val of n present if yes pick from there this will save time - if cache[n] != None: - return cache[n] - - # Recursive call to and keep setting cache - cache[n] = fibonacci_dynamic(n - 1) + fibonacci_dynamic(n - 2) + # Check cache if already val of n present if yes pick from there this will save time + if cache[n] != None: return cache[n] + + # Recursive call to and keep setting cache + cache[n] = fibonacci_dynamic(n - 1) + fibonacci_dynamic(n - 2) + return cache[n] + # Test - # We'll try to find the 9th no in the fibnacci sequence which is 34 - print (fibonacci_dynamic(9)) - # 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 - # The recursive-memoization solution is exponential time Big-O , with O(n) \ No newline at end of file +# We'll try to find the 9th no in the fibnacci sequence which is 34 +print(fibonacci_dynamic(9)) +# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 +# The recursive-memoization solution is exponential time Big-O , with O(n) diff --git a/Recursion/README.md b/Recursion/README.md new file mode 100644 index 0000000..29b67c1 --- /dev/null +++ b/Recursion/README.md @@ -0,0 +1,21 @@ +# Recursion + +This directory contains problems and solutions using recursive algorithms. + +## Contents + +### Basic Recursion Problems +- [RecursionCumulativeSum.py](RecursionCumulativeSum.py) +- [RecursionSumOfDigits.py](RecursionSumOfDigits.py) +- [RecursionReverseStr.py](RecursionReverseStr.py) +- [RecursionStrPermutation.py](RecursionStrPermutation.py) +- [RecursionWordSplit.py](RecursionWordSplit.py) + +### Fibonacci Sequence +- [FibonacciSeqIterative.py](FibonacciSeqIterative.py) +- [FibonacciSeqRecursion.py](FibonacciSeqRecursion.py) +- [FibonacciSeqDynamic.py](FibonacciSeqDynamic.py) + +### Coin Change Problem +- [CoinChangeProblemRecursion.py](CoinChangeProblemRecursion.py) +- [CoinChangeProblemDynamic.py](CoinChangeProblemDynamic.py) diff --git a/Recursion/RecursionReverseStr.py b/Recursion/RecursionReverseStr.py index 25bf1a3..5cd6ef6 100644 --- a/Recursion/RecursionReverseStr.py +++ b/Recursion/RecursionReverseStr.py @@ -12,8 +12,8 @@ def reverse_str(str): return str else: # Implement recursive reverse - # Take first index of the string and then add rest starts from index 1 and do a recursive call - return reverse_str(str[1:] + str[0]) + # Reverse the remainder of the string and then add the first character to the end + return reverse_str(str[1:]) + str[0] # Test print(reverse_str('hello world')) diff --git a/Recursion/RecursionStrPermutation.py b/Recursion/RecursionStrPermutation.py index 5625efb..245574c 100644 --- a/Recursion/RecursionStrPermutation.py +++ b/Recursion/RecursionStrPermutation.py @@ -35,8 +35,8 @@ def permute(str): # so grab everything till that index and then index + 1 till the end for perm in permute(str[:i] + str[i + 1:]): # Add it to output ( letter and its permutation) - print 'Current letter is: ',cur_letter - print 'Perm is: ',perm + # print('Current letter is: ', cur_letter) + # print('Perm is: ', perm) out += [cur_letter + perm] return out diff --git a/Recursion/RecursionSumOfDigits.py b/Recursion/RecursionSumOfDigits.py index 210295c..387846c 100644 --- a/Recursion/RecursionSumOfDigits.py +++ b/Recursion/RecursionSumOfDigits.py @@ -4,20 +4,18 @@ # Given an integer, create a function which returns the sum of all the individual # digits in that integer. For example: if n = 4321, return 4+3+2+1 -# Implement recusion sum routine +# Implement recursion sum routine def recursion_sum_digits(n): - # Checking edge case is really important - str_n = str(n) - print (str_n) - if len(str_n) == 0: + # Base case: if n is less than 10, it's a single digit + if n < 10: return n - #print (n) - # Recursion, use modulo operator to get RMD and then use divide to pass - # to recursive routine next interation repeat same procedure - # with small number and add + # Recursion: use modulo operator to get last digit and then use floor divide + # to pass the rest to recursive routine else: - return n%10 + recursion_sum_digits(n/10) + return n % 10 + recursion_sum_digits(n // 10) # Test -print (recursion_sum_digits(12)) -# Result = 9+8+5+4 = 25 \ No newline at end of file +print(recursion_sum_digits(4321)) +# Result = 4+3+2+1 = 10 +print(recursion_sum_digits(12)) +# Result = 1+2 = 3 diff --git a/Sorting/README.md b/Sorting/README.md new file mode 100644 index 0000000..9336368 --- /dev/null +++ b/Sorting/README.md @@ -0,0 +1,29 @@ +# Sorting Algorithms + +This directory contains implementations of common sorting algorithms. + +## Contents + +### Bubble Sort +- [BubbleSortImple.py](BubbleSortImple.py) +- Complexity: O(n²) worst/average, O(n) best. + +### Selection Sort +- [SelectionSortImple.py](SelectionSortImple.py) +- Complexity: O(n²) for all cases. + +### Insertion Sort +- [InsertionSortImple.py](InsertionSortImple.py) +- Complexity: O(n²) worst/average, O(n) best. + +### Merge Sort +- [MergeSortImple.py](MergeSortImple.py) +- Complexity: O(n log n). + +### Quick Sort +- [QuickSortImple.py](QuickSortImple.py) +- Complexity: O(n²) worst, O(n log n) average. + +### Shell Sort +- [ShellSortImple.py](ShellSortImple.py) +- Complexity: O(n) to O(n²). diff --git a/Stacks/README.md b/Stacks/README.md new file mode 100644 index 0000000..6bbaefa --- /dev/null +++ b/Stacks/README.md @@ -0,0 +1,13 @@ +# Stacks + +This directory contains implementations and problems related to stack data structures. + +## Contents + +### Stack Implementation +Basic stack operations (LIFO): `push`, `pop`, `peek`, `isEmpty`, `size`. +- [StackImple.py](StackImple.py) + +### Balanced Parentheses Check +Check if a string of opening and closing parentheses is balanced. +- [BalanceParenthlessCheckImple.py](BalanceParenthlessCheckImple.py) diff --git a/Trees/README.md b/Trees/README.md new file mode 100644 index 0000000..eada76a --- /dev/null +++ b/Trees/README.md @@ -0,0 +1,25 @@ +# Trees + +This directory contains implementations and problems related to tree and heap data structures. + +## Contents + +### Binary Search Tree +- [BinarySearchTreesImple.py](BinarySearchTreesImple.py) +- [BinarySearchTreeCheckImpleSol1.py](BinarySearchTreeCheckImpleSol1.py) (Check if tree is valid BST) +- [BinarySearchTreeCheckImpleSol2.py](BinarySearchTreeCheckImpleSol2.py) (Check if tree is valid BST) +- [TrimBinarySearchTreeImple.py](TrimBinarySearchTreeImple.py) + +### Binary Heap +- [BinaryHeapImple.py](BinaryHeapImple.py) + +### Binary Search +- [BinarySearchImple.py](BinarySearchImple.py) +- [BinarySearchRecursiveImple.py](BinarySearchRecursiveImple.py) + +### Tree Representation +- [TreeRepresentationWithNodesReferences.py](TreeRepresentationWithNodesReferences.py) +- [buildTreeTest.py](buildTreeTest.py) + +### Tree Level Order Print +- [TreeLevelOrderPrintImple.py](TreeLevelOrderPrintImple.py) diff --git a/deque/README.md b/deque/README.md new file mode 100644 index 0000000..628680f --- /dev/null +++ b/deque/README.md @@ -0,0 +1,9 @@ +# Deque + +This directory contains double-ended queue (deque) implementations. + +## Contents + +### Deque Implementation +Double-ended queue operations: `addFront`, `addRear`, `removeFront`, `removeRear`, `isEmpty`, `size`. +- [DequeImple.py](DequeImple.py)