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 / 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 / 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 / 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 : 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:
#!/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 / 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
@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 / 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 / KMP.py
Created June 12, 2018 15:44
Finding a specific pattern in a text using the Knuth Morris Pratt pattern searching algorithm
def kmp(txt, pat):
# base conditions
if len(pat) == len(txt):
if pat == txt:
return 0
else:
return -1
if len(pat) > len(txt):
return -1
if len(pat) == 0 or len(txt) == 0:
@rajatdiptabiswas
rajatdiptabiswas / Dynamic Programming : Text Justification.py
Last active June 21, 2018 11:55
Find the position of line breaks in a given string such that the lines are printed neatly. The string and the number of characters that can be put in one line are given as inputs. (https://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/) (https://www.youtube.com/watch?v=RORuwHiblPc)
#!/usr/bin/env python3
import math
def main():
# take inputs
string = input("Enter the text to be justified:\n").split()
width = int(input("Enter the width of the line: "))