15-112 Fall 2012 Quiz 7 (Autograded)
45 minutes


Read these instructions first!
  1. missingValues [25 pts]
  2. isRectangular [25 pts]
  3. removeRowAndCol [25 pts]
  4. destructiveRemoveRowAndCol [25 pts]
  5. bonus/optional: findMagicValue [5 pts bonus]

  1. missingValues [25 pts]
    Write the function missingValues(D,S) that takes a dictionary D and a set S, and returns the possibly-empty set of all the values (not keys) in the dictionary D that are not in the set S.  For example:
        D = { "A": 2, "B": 3, "C": 5, "D": 7 }
        S = set([2, 5, 9, 11])
        T = set([3, 7])
        assert(missingValues(D, S) == T)
  2. isRectangular [25 pts]
    Write the function isRectangular(L) that takes a list L and returns True if it is a rectangular 2d List (so every value in L is a list representing a row, and every row has the same number of columns) and False otherwise.  Look at the test cases to carefully consider some edge cases.
        assert(isRectangular([[1,2],[3,4]]) == True)
        assert(isRectangular([[1,2],[3,4,5]]) == False)
        assert(isRectangular(["this", "is", "silly"]) == False)
        assert(isRectangular([[1],[2]]) == True)
        assert(isRectangular([[],[]]) == True)
        assert(isRectangular([]) == False)
  3. removeRowAndCol [25 pts]
    Write the function removeRowAndCol(A, row, col) that takes a 2d list A, a row index and a col index, and non-destructively returns a new 2d list with the given row and column removed.  So, if:
        A = [ [ 2, 3, 4, 5],
              [ 8, 7, 6, 5],
              [ 0, 1, 2, 3]
            ]
    Then removeRowAndCol(A, 1, 2) returns:
            [ [ 2, 3, 5],
              [ 0, 1, 3]
            ]
    Also, A remains unchanged.

    You may assume that row and col are in range (that A is a 2d list and A[row][col] exists).

    Here are some test cases for you to consider:
        A = [ [ 2, 3, 4, 5],
              [ 8, 7, 6, 5],
              [ 0, 1, 2, 3]
            ]
        Acopy = copy.copy(A)
        B = [ [ 2, 3, 5],
              [ 0, 1, 3]
            ]
        assert(removeRowAndCol(A, 1, 2) == B)
        assert(A == Acopy) # so A was unchanged
        A = [ [ 1, 2 ], [3, 4] ]
        B = [ [ 4 ] ]
        assert(removeRowAndCol(A, 0, 0) == B)
        A = [ [ 1, 2 ] ]
        B = [ ]
        assert(removeRowAndCol(A, 0, 0) == B)
  4. destructiveRemoveRowAndCol [25 pts]
    Write the function destructiveRemoveRowAndCol(A, row, col) that works the same as the previous problem, only it is destructive, so it returns None and instead directly modifies A to remove the given row and col.
     
  5. bonus/optional: findMagicValue [5 pts bonus]
    Write the function findMagicValue() so that the following code returns True:
    def testBonusFindMagicValue():
        try:
            (b,D) = (3,findMagicValue())
            A = [set() for i in xrange(b)]
            for value in xrange(2**b):
                for i in xrange(b):
                    if (((value>>i)&0b1) == 1):
                        A[i].add(value)
            C = set(range(2**b+1)) - set(range(0,2**b+1,2))
            for i in xrange(b):
                b -= (D.get(i,42) == sorted(A[i] - C))
            ok = (not b)
        except:
            ok = False
        return ok

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem