CMU 15-112 Fall 2015 Quiz 5 Practice: Code Tracing
(Due never)
- This is part of quiz5-practice.
- Quiz5 code tracing will be nearly identical to these.
# Code Tracing #1 of 2
import copy
def p(a): return [a[row][0] for row in range(len(a))]
def ct1(a):
(b, c) = (copy.copy(a), copy.deepcopy(a))
a[0] = b[1]
b[2] = c[0]
c[1] = a[2]
a[0][0] = 7
b[2][0] = 8
c[1][0] = 9
print(p(a), p(b), p(c), end = " ")
a = [[4], [5], [6]]
ct1(a)
print(p(a))
# Code Tracing #2 of 2
def ct2(a):
(rows, cols) = (len(a), len(a[0]))
result = [0]*rows
for row in range(rows):
for col in range(0,cols,2): #note the range!
result[row] += a[rows-1-row][col] # note indices!
return result
print(ct2([[1,2,3,4,5],
[2,4,6,8,10]]))