This file contains hidden or 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 tower_of_hanoi(number, beginning, auxiliary, ending): | |
if number == 1: | |
print("{} -> {}".format(beginning, ending)) | |
return | |
else: | |
tower_of_hanoi(number-1, beginning, ending, auxiliary) |
This file contains hidden or 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
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 |
This file contains hidden or 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 | |
""" | |
Binary Indexed Tree / Fenwick Tree | |
https://www.hackerearth.com/practice/notes/binary-indexed-tree-made-easy-2/ | |
https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/ | |
https://www.youtube.com/watch?v=v_wj_mOAlig | |
https://www.youtube.com/watch?v=kPaJfAUwViY | |
""" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |