Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Arrays/README.md
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions Error-debug/README.md
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions GraphAlgorithms/README.md
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions LinkedLists/README.md
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions Queues/README.md
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
```

---

Expand Down Expand Up @@ -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.
35 changes: 18 additions & 17 deletions Recursion/FibonacciSeqDynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# 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)
21 changes: 21 additions & 0 deletions Recursion/README.md
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions Recursion/RecursionReverseStr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
4 changes: 2 additions & 2 deletions Recursion/RecursionStrPermutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions Recursion/RecursionSumOfDigits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
print(recursion_sum_digits(4321))
# Result = 4+3+2+1 = 10
print(recursion_sum_digits(12))
# Result = 1+2 = 3
29 changes: 29 additions & 0 deletions Sorting/README.md
Original file line number Diff line number Diff line change
@@ -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²).
13 changes: 13 additions & 0 deletions Stacks/README.md
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions Trees/README.md
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions deque/README.md
Original file line number Diff line number Diff line change
@@ -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)