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
from math import sqrt, pow | |
class ATaleOfThreeCities: | |
AB = 1 | |
BC = 2 | |
CA = 3 | |
def __init__(self, min1=999, min2=999): | |
self.min1 = min1 |
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
from collections import deque | |
def permutations(word): | |
if len(word) == 1: | |
return [word] | |
perms = permutations(word[1:]) | |
c = word[0] | |
res = [] |
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
from collections import deque | |
def permutations(word): | |
if len(word) == 1: | |
return [word] | |
perms = permutations(word[1:]) | |
c = word[0] | |
res = [] |
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
class BadNeighbours: | |
def maxDonations(self, donations): | |
L = len(donations) | |
if L < 3: | |
return max(donations) | |
# memo1[L-2] will have maximum value of donations which | |
# *may* include the first neighbour (therefore cannot include |
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
class AvoidRoads: | |
def badDict(self, bad): | |
bd = {} | |
for b in bad: | |
bd[b] = True | |
return bd | |
def numWays(self, width, height, bad): |
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
class State: | |
def __init__(self, longest_sequence, next_diff): | |
""" next_diff = 1 means the next expected difference | |
in the zigzag sequence is positive | |
next_diff = 0 means the next expected difference | |
in the zigzag sequence is negative """ | |
self.longest = longest_sequence | |
self.next_diff = next_diff |
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
from collections import deque | |
class Square: | |
def __init__(self, col, row, prev=None, moves=9999): | |
self.col = col | |
self.row = row | |
self.prev = prev | |
self.moves = moves | |
def set_moves(self, moves): |
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
class Vertex: | |
def __init__(self, word, dist=9999): | |
self.word = word | |
self.dist = dist | |
def gen_all_neighbours(v, forbidden): | |
""" Generate all neighbours for a vertex, i.e. all | |
words that are one letter off. Uses ascii values and | |
excludes any words in list(forbidden). | |
""" |
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 swap(S, i1, i2): | |
l = list(S) | |
l[i1], l[i2] = l[i2], l[i1] | |
S = ''.join(l) | |
return S | |
def prependBs(S, N): | |
nBs = N - len(S) | |
return "B"*nBs + S |
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
# you can write to stdout for debugging purposes, e.g. | |
# print "this is a debug message" | |
def solution(A): | |
# write your code in Python 2.7 | |
L = len(A) | |
n = 0 | |
if L == 1: | |
return 0 |