15-112 Fall 2011 Quiz 6
20 Minutes.  No calculators, no notes, no books, no computers.

1.       [60 pts]  On the back of this sheet, based on our wordSearch case study, write a modified version of this function:
    wordSearch2(puzzle, word, startRow, startCol, drow, dcol)
Your modification:  words can wraparound, so as you exit the puzzle on one side, you re-enter on the opposite side, still heading in the same direction.   For example, consider this puzzle:
puzzle = [ [ 'q', 'r', 'w' ],
           [ 'o', 'd', 'g' ],
           [ 'm', 'c', 'c' ]
         ]

This puzzle contains the word “dog” (starting at (1,1), heading west, with wraparound), and the word “cow” (starting at (2,1), heading northwest, with wraparound).  You may not assume any helper functions already exist, so you must write any helper functions your solution requires.

2.        [20 pts]  Right here (not on the back of this sheet), based on our connect4 case study, write this 2-line function:
    checkForWin(puzzle, player)
Here, you may assume any helper functions that we used already exist, and you may not write more than 3 lines.
 

3.       [20 pts; 5 pts each]  Very briefly answer the following:
a)  Give an example of a list a such that copy.copy(a) works differently than copy.deepcopy(a).

b) Write a Python assert that checks that the non-ragged 3d list a is cubic (same-sized in all 3 dimensions).


c) For each expression, indicate exactly how many distinct (non-alias) 1d or 2d lists are created:
        1)   [ ([0] * cols) for row in xrange(rows) ]
        2)   [ [0] * cols ] * rows

d) Is it easier to send a row or a column of a 2d list to a function?  And, in 10 words or less, why?
 

4.       Bonus/Optional [5pts]:  What will the following code print?  Hint: it prints exactly 10 lines, each worth ˝ point.
def f(a):
    (j,a0) = (0, a+[])
    for b in a0:
        for c in b:
            for i in xrange(len(a0)):
                try: a[i][j] += c; print i,j,a[i][j]
                except: j = -1
                finally: j += 1
    print a
f([[-2,-1,0],[1,2,3]])