Skip to content

Instantly share code, notes, and snippets.

View rajatdiptabiswas's full-sized avatar
:octocat:
Octocat says Git Gud

rajat rajatdiptabiswas

:octocat:
Octocat says Git Gud
View GitHub Profile
@rajatdiptabiswas
rajatdiptabiswas / LPS : Manacher's Algorithm.py
Created June 12, 2018 15:36
Finding the Longest Palindromic Substring using Manacher's Algorithm in O(n) time complexity
'''
There are 4 cases to handle
* Case 1 : Right side palindrome is totally contained under current palindrome. In this case do not consider this as center.
* Case 2 : Current palindrome is proper suffix of input. Terminate the loop in this case. No better palindrome will be found on right.
* Case 3 : Right side palindrome is proper suffix and its corresponding left side palindrome is proper prefix of current palindrome. Make largest such point as next center.
* Case 4 : Right side palindrome is proper suffix but its left corresponding palindrome is be beyond current palindrome. Do not consider this as center because it will not extend at all.
'''
def longest_palindromic_substring(string):
# preprocess the string to allow for even length palindromes
@rajatdiptabiswas
rajatdiptabiswas / LPS : Dynamic Programming.py
Created June 12, 2018 13:35
Finding the Longest Palindromic Substring using Dynamic Programming in O(n^2) time complexity
def longest_palindromic_substring(string):
length = len(string)
# return the string if the string is only one character long
if length == 1:
return string
palindrome = ""
palindrome_length = 0
@rajatdiptabiswas
rajatdiptabiswas / Sieve of Eratosthenes.py
Last active June 8, 2018 16:12
Finding primes using the Sieve of Eratosthenes
#!/usr/bin/env python3
def sieve_of_eratosthenes(n):
'''
Complexity : O(n log log n) + O(n) ~ O(n log log n)
'''
prime = [True for i in range(n+1)]
p = 2
# if there are no factors of n till sqrt(n) then the number is prime
#!/usr/bin/env python3
class Node(object):
"""Class for tree nodes"""
def __init__(self, key):
super(Node, self).__init__()
self.key = key
self.left = None
@rajatdiptabiswas
rajatdiptabiswas / Backtracking : Sudoku Solver.py
Created April 8, 2018 20:26
Given a partially filled 9×9 2D array, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9 (https://www.geeksforgeeks.org/backtracking-set-7-suduku/)
#!/usr/bin/env python3
"""
Sudoku Solver
guesses:
1-9
grid:
@rajatdiptabiswas
rajatdiptabiswas / Backtracking : Rat in a Maze.py
Created April 8, 2018 20:23
Problem where a rat starts from source and has to reach the destination (https://www.geeksforgeeks.org/backttracking-set-2-rat-in-a-maze/)
#!/usr/bin/env python3
def is_safe(x, y, n, maze):
if 0 <= x <= n-1 and 0 <= y <= n - 1 and maze[y][x] == 1:
return True
else:
return False
@rajatdiptabiswas
rajatdiptabiswas / Backtracking : N Queens.py
Last active April 8, 2018 20:21
Problem of placing N chess queens on an N×N chessboard so that no two queens attack each other (https://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/)
#!/usr/bin/env python3
def is_safe(row, col, size, board):
# check vertical
for y in range(row):
if board[y][col]:
return False
# check left diagonal
@rajatdiptabiswas
rajatdiptabiswas / Memoization.py
Last active March 24, 2018 10:53
The memoization decorator in Python to solve Dynamic Programming problems
#!/usr/bin/env python3
def memoization(function):
memo = {}
def helper(x):
if x not in memo:
memo[x] = function(x)
return memo[x]
@rajatdiptabiswas
rajatdiptabiswas / 2D Point Class.py
Created March 1, 2018 12:39
A point class with x and y coordinates for 2D planes
import math
class Point(object):
'''Creates a point on a coordinate plane with values x and y'''
def __init__(self, x, y):
'''Defines the x and y coordinates in the variables'''
self.X = x
self.Y = y
@rajatdiptabiswas
rajatdiptabiswas / Tower of Hanoi.py
Last active February 14, 2018 08:59
Python program to solve the Tower of Hanoi problem recursively
#!/usr/bin/env python3
def tower_of_hanoi(number, beginning, auxiliary, ending):
if number == 1:
print("{} -> {}".format(beginning, ending))
return
else:
tower_of_hanoi(number-1, beginning, ending, auxiliary)