L = copy.deepcopy(L) doDestructiveVersion(L) return LInstead, do not use copy.deepcopy and directly construct the modified 2d list.
list | result | |
[ [ 2, 3, 4, 5], [ 8, 7, 6, 5], [ 0, 1, 2, 3] ] |
[ [ 2, 3, 5], [ 0, 1, 3] ] |
None
.
[ [ 3, 2, 1 ], [ [ 1, 2, 3 ], [ [ 3, 2, 1 ], [ 6, 4, 9 ], [ 7, 4, 8 ], [ 6, 4, 0 ], [ 5, 7, 8 ] ] [ 6, 5, 9 ] ] [ 5, 7, 8 ] ]The first is a legal Kings Tour but the second is not, because there is no way to legally move from the 7 to the 8, and the third is not, because it contains a 0 which is out of range. Also, this should work not just for 3x3 boards but for any NxN board. For example, here is a legal Kings Tour in a 4x4 board:
[ [ 1, 14, 15, 16], [ 13, 2, 7, 6], [ 12, 8, 3, 5], [ 11, 10, 9, 4] ]With this in mind, write the function isKingsTour(board) that takes a 2d list of integers, which you may assume is NxN for some N>0, and returns True if it represents a legal Kings Tour and False otherwise.
board = [ [ 'p', 'i', 'g' ], [ 's', 2, 'c' ], ]When matching a word, a positive integer on the board matches exactly that many letters in the word. So the board above contains the word "cow" starting from [1,2] heading left, since the 2 matches "ow". It also contains the word "cows" for the same reason. To make this work, of the three functions in our wordSearch solution, the outer two do not have to change (except a slight renaming), but the innermost one does. Add all three functions to your hw file, renaming the outermost function to wordSearchWithIntegerWildCards, and then rewrite the innermost function here so that it works as described.
im | panImage(im, -2, 0, 88) | |
[ [ 10, 11, 12, 13], [ 14, 15, 16, 17], [ 18, 19, 20, 21] ] |
[ [ 12, 13, 88, 88], [ 16, 17, 88, 88], [ 20, 21, 88, 88] ] |
def testMakeWordSearch():
print("Testing makeWordSearch()...", end="")
board = makeWordSearch([], False)
assert(board == None)
board = makeWordSearch(["ab"], False)
assert(board == [['a', 'b'], ['-', '-'] ])
board = makeWordSearch(["ab"], True)
assert(board == [['a', 'b'], ['c', 'd'] ])
board = makeWordSearch(["ab", "bc", "cd"], False)
assert(board == [['a', 'b'], ['c', 'd'] ])
board = makeWordSearch(["ab", "bc", "cd", "de"], False)
assert(board == [['a', 'b', '-'], ['c', 'd', '-'], ['d', 'e', '-']])
board = makeWordSearch(["ab", "bc", "cd", "de"], True)
assert(board == [['a', 'b', 'a'], ['c', 'd', 'c'], ['d', 'e', 'a']])
board = makeWordSearch(["abc"], False)
assert(board == [['a', 'b', 'c'], ['-', '-', '-'], ['-', '-', '-']])
board = makeWordSearch(["abc"], True)
assert(board == [['a', 'b', 'c'], ['c', 'd', 'a'], ['a', 'b', 'c']])
board = makeWordSearch(["abc", "adc", "bd", "bef", "gfc"], False)
assert(board == [['a', 'b', 'c'], ['d', 'e', '-'], ['c', 'f', 'g']])
board = makeWordSearch(["abc", "adc", "bd", "bef", "gfc"], True)
assert(board == [['a', 'b', 'c'], ['d', 'e', 'a'], ['c', 'f', 'g']])
print("Passed!")