if (solution != None): return solution return None
def wordSearch2(puzzle, word, startRow, startCol, drow, dcol): rows = len(puzzle) cols = len(puzzle[0]) for i in xrange(len(word): cWord = word[i] pu
1. Example Using Sets 2. def repeats(a): 3. 4. 5. 6. 7. 8. 9.
10. print repeats([2,5,3,4,6,4,2]) # prints [2,4] seen = set() seenAgain = set() for element in a: if (element in seen): seenAgain.add(ele
1. Set Operations
See tables here.
1. Operations on a set
Operation
len(s) s.copy() s.pop() s.clear()
Result cardinality of set s new set with a shallow copy of s remove and return an arbitrary elemen
1. s = set([2,True,"yes"])
print s # prints set([True, 2, 'yes'])
2. Elements Must Be Immutable 3. a = ["lists", "are", "mutable"] 4. s = set([a]) 5. print s
6. # TypeError: unhashable type: 'list'
7.
pointList = [] y=0 for x in xrange(1,k+3): #we removed 0 as the first element to avoid a row swap, so this becomes k+3 instead of k+2 # y = 1*k + . + n*k y += x*k pointList += [(x,y)] return fitExactP
So, let's turn this into code.
def fitExactPolynomial(pointList): n = len(pointList) #n=number of points degree = n - 1 # 1. make A A = make2dList(n,n) #allocating space for row in xrange(n): #row det
def invertMatrix(m): n = len(m) assert(len(m) = len(m[0]) inverse = makeIdentity(n) for col in xrange(n): #proceeds 1 column at a time # 1. make the diagonal contain a 1 diagonalRow = col k = 1.0 / m[
def checkForWin(puzzle, player): winningWord = player * 4 return (wordSearch(puzzle, winningWord) != None) # that was easy!
# # taken from wordSearch.py #
# wordSearch.py # This code was developed in
moveRow = getMoveRow(puzzle, moveCol) puzzle[moveRow][moveCol] = player printPuzzle(puzzle) if checkForWin(puzzle, player): print "* Player Wins! *" % player return moveCount += 1 player = "O" if (pl