15-112 Fall 2013 Quiz 5
* 25 Minutes.  No calculators, no notes, no books, no computers.  * Show your work. Circle your answers.

1.       Code Tracing [15 pts]  Indicate what this will print.
For this question only:  you may use T for True and F for False, and you may omit all brackets [ ] in your answers.

a = [[1],[2]]

(b,c,d) = (a, copy.copy(a), copy.deepcopy(a))

print (a == b), (a == c), (a == d)

print (a is b), (a is c), (a is d)

print (a[0] is b[0]), (a[0] is c[0]), (a[0] is d[0])

 

a = [[1],[2]]

print "A",

try:

    print "B",

    b[0][0] += 10

    c[1][0] += 100

    d[1][1] += 1000

    a[0][0] += 10*1000

    print "C",

except:

    print "D",

print "E"

for L in [a,b,c,d]: print L

 

print "F"

a = [[1],[2]]

(b,c,d) = (a, copy.copy(a), copy.deepcopy(a))

a[0][0] += 1

b[0][0] += 2

c[1][0] += 3

d[1][0] += 4

a[0] = c[0]

b[0] = d[0]

for L in [a,b,c,d]: print L
 

2.    Reasoning Over Code [10 pts]
Find arguments for the following functions that make each return True.

def f(L):

    # Assume L is a ragged (non-rectangular!) 2d list

    return (max(L[0]) == 2) and L == [range(len(L)-n) for n in xrange(len(L))]

def g(s1, s2):

    d = dict()

    for val in s1: d[val] = 1

    for val in s2: d[val] = 2 + d.get(val,0)

    return d == { 2:3, 3:2, 0:1 }
 

3.       Free Response:  Word Search with Integer wildcards [75 pts]
Here we will modify wordSearch so that we can include positive integers in the board, like so (see board[1][1]):
    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.  But it does not contain the word “co”, since the 2 must match exactly 2 letters.  To make this work,  of the three functions in our wordSearch solution, the outer two do not have to change, but the innermost one does.  Rewrite the innermost function here so that it works as described.  Among other parameters, your function should take a direction as a non-negative int (rather than a tuple), and it should return True if the word is in the wordSearch and False otherwise.