CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Week 2 (Due never) Code Tracing (CT) Exercises


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

  1. def ct1(s, t): result = "" for c in s: if (c.upper() not in "NO!!!"): i = t.find(c) if (result != ""): result += ":" result += f"{i}{c}{s[i]}{t[i]}" return result print(ct1("net", "two"))

  2. def ct2(s): result = "" d = ord("a") for c in s.lower(): if (c.isalpha() and (ord(c) >= d)): result += str(ord(c) - d) + chr(d) d += 1 return result print(ct2("Be a CA?!?"))

  3. def ct3(s): result = "" while (len(s) > 1): result += s[:1] + s[2:4] + "." s = s[1:-1:2] return result + s print(ct3("abcdefghi"))

  4. def ct4(s, n): result = "" d = 0 while (n > 0) and (len(s) > 0): if (s[-1].isdigit()): result += str((n%10)%int(s[-1])) else: result += chr(ord('D')+d) n //= 10 s = s[1:-1] d += 1 return result print(ct4("abc3c3", 2468))