CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Unit 5 (Due never) Code Tracing (CT) and Reasoning Over Code (ROC) Exercises


These problems will help you prepare for the upcoming midterm. They are optional and you are encouraged to collaborate when working on them.
    Code Tracing (CT)
    What will each of these print?

    1. Trace #1:
      def onesDigit(n): return n%10 def ct1(L): for i in range(len(L)): L[i] += sum(L) + max(L) return sorted(L, key=onesDigit) a = [2,1,0] print(ct1(a)) print(a)

    2. Trace #2:
      def ct2(a, b): a += [3] a = a + [4] for c in a: for i in range(len(b)): if (b[i] not in a): print("A", end="") b[i] += c elif (c % 2 == 1): print("B", end="") b += [c] elif (b[-1] != c): print("C", end="") b = b + [c] return (a,b) a = [4] b = [2,3] print(ct2(a,b)) print(a,b)

    3. Trace #3:
      def ct3(L): result = [ ] M = [L[i]*10**i for i in range(len(L))] for val in M: result.extend([val, L.pop()]) return result L = [2,5,3] M = ct3(L) print(L, M)

    4. Trace #4:
      def ct4(L): result = [ ] M = L[:] # same as M = copy.copy(L) if (M == L): result.append(1) if (M is L): result.append(2) if (M[0] == L[0]): result.append(3) if (M[0] is L[0]): result.append(4) return result print(ct4([5,7,6]))

    5. Trace #5:
      def ct5(L): M = L L += [4] M = M + [5] print(L, M) ct5(list(range(1)))

    6. Trace #6:
      def ct6(): print([3,2,1] > [3,2]) a = [1] b = a c = copy.copy(a) a += [2] a = a + [3] print(a, b, c) a = [2,3,5,3,2] a.remove(2) v = a.pop(2) print(a, v) L = list(range(1,7,2)) M = [v**2 for v in L] N = [v*10 for v in M if v < 10] print(L, M, N) L = ['A', 'BC', 'D'] print('xy'.join(L)) ct6()

    7. Trace #7:
      import copy a = [1,2,3,4] (b,c) = (a, copy.copy(a)) print((a == b), (a == c)) print((a is b), (a is c)) print((a[0] is b[0]), (a[0] is c[0])) a = [1,2] (b,c) = (a, copy.copy(a)) b[0] += 10 c[1] += 100 for L in [a,b,c]: print(L) a = [1,2] (b,c) = (a, copy.copy(a)) a[0] += 1 b[0] += 2 c[1] += 3 a[0] = c[0] b[0] = a[1] for L in [a,b,c]: print(L) print() a = list(range(2,10,7)) (b, c) = (a, a[:2]) b[0] += 3 a += [3] a = a + [4] print(c + [b[0]]) print(c.append(b[1])) print(a, b, c)

    8. Trace #8:
      import copy def f(a, b): a = copy.copy(a) a[0] = b[1] b[0] = a[1] return a + b a1 = a2 = list(range(5,7)) b1 = b2 = list(range(2)) a1 = f(a1, b1) print(a1, a2, b1, b2)

    9. Trace #9:
      def onesDigit(n): return n%10 def ct(L): for i in range(len(L)): L[i] += sum(L) + max(L) return sorted(L, key=onesDigit) a = [2,1,0] print(ct(a)) print(a)

    10. Trace #10:
      def ct(a): a[1] = "foo" a[0], a[2] = a[2], a[0] print(a) b = a b.extend([42, "wow" ]) b.pop() print(b) c = [] + b c[1:-1] = ["Hello" , "World" ] return c lst = [15, "1" , "twelve" ] print(ct(lst)) print(lst)

    Reasoning Over Code (ROC)
    Find values for the parameters so the functons return True:

    1. RC #1:
      def rc1(M): assert(isinstance(M, list) and (len(M) == 5)) for i in range(-1, 3): assert(M[i] == M[i-1] + i) return (sum(M) == 15)

    2. RC #2:
      def rc2(L): assert((isinstance(L, list)) and (None not in L)) i = 0 while (L[i] != None): j = L[i] L[i] = None i = j a = [None]*2 return (L == a + [-1] + a)

    3. RC #3:
      def rc1(L): result = [ ] for i in range(len(L)): m = L[i] for j in range(i, len(L)): if (L[j] > m): m = L[j] result.append(m) return (result == [3, 3, 2, 1])

    4. RC #4:
      import copy def rc(a): assert(sorted(a) == list(range(5,9))) b = copy.copy(a) for i in range(len(b)): b[len(b)-1-i] *= i return ((b[0] == 18) and (min(a) == b[2]) and (a[1] > a[3]))