CMU 15-110: Principles of Computing
Week6 Practice (Due never)
Contents:
1. Code Tracing
What will this code print? Figure it out by hand, then run the code to confirm. Then slightly edit the code and try again.
- Trace #1:
def ct1(L): while (len(L) > 1): a = L.pop() b = L.pop() L.append(max(a,b)) return L[0] a = [4,5,2,3,4,1] print(ct1(a)) print(a)
- Trace #2:
def ct2(L): L[0] += 5 L = L + [ 5 ] L[1] += 5 for i in range(len(L)): L[i] += sum(L) return L a = [1, 2] print(ct2(a)) print(a)
- Trace #3:
def ct3(s, c): a = s.split(c) b = [ len(a) ] for w in a: if (w.isalpha() == True): b.append(w) return b print(ct3('This, this is a test, yes, a test!', ' ')) print(ct3('This, this is a test, yes, a test!', ','))
- More 1d-List Practice
See here