This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def memoization(function): | |
memo = {} | |
def helper(x): | |
if x not in memo: | |
memo[x] = function(x) | |
return memo[x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Sudoku Solver | |
guesses: | |
1-9 | |
grid: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: ")) | |