CMU 15-110: Principles of Computing
Week4 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 f(s): t = s + s.replace('th', '') t += chr(ord('a') + len(s)) for c in 'unshift': if (c not in t): t += c return t print(f('this '))
- Trace #2:
def encode(s): result = '' for c in s: result += chr(ord(c) + 1) return result print(encode('abc xyz 123')) # note: chr(32) is ' ', chr(33) is '!', chr(123) is '{'
- Trace #3:
def decode(s): result = '' for c in s: result += chr(ord(c) - 1) return result print(decode('bcd!yz{!234'))