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


These problems will help you prepare for hw3 and quiz3. 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 += "%d%s%s%s" % (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))

  5. # Graphics-CT #1 # Draw a picture on a piece of paper showing what this draws when it runs. import basic_graphics def draw(canvas, width, height): # assume (width,height) == (300,300) canvas.create_rectangle(10, 10, width-10, height-10, fill=None) (cx, cy, d) = (width*2/3, height/3, width/10) L = [ ] for i in range(-2,3): (dx, dy, r) = (cx+d*i, cy+abs(d*i), d/2) canvas.create_line(dx, dy, cx, cy+100) canvas.create_oval(dx-r, dy-r, dx+r, dy+r, fill="gray") L.append((dx, 30+10*i**2)) canvas.create_polygon(L, fill="gray") (x0, y0, x1, y1) = (20, 20, 100, 200) canvas.create_rectangle(x0, y0, x1, y1, fill="gray") canvas.create_oval(x0, y0, x1, y1, fill="white") textY = height-60 canvas.create_line(20, textY, width-20, textY) canvas.create_text(width/2, textY, text="CT1", font="Arial 16", anchor="s") canvas.create_text(width/2, textY, text="(What will this draw?)", font="Arial 16", anchor="n") basic_graphics.run()

  6. # Graphics-CT #2 # Draw a picture on a piece of paper showing what this draws when it runs. import basic_graphics def draw(canvas, width, height): (x1,y1) = (200,0) for x in range(50,500,200): for y in range(x, 400, 150): canvas.create_line(x,y,x1,y1) canvas.create_text(x,y,text=str((x,y))) (x1,y1) = (x,y) basic_graphics.run()

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

  1. def rc1(n): assert(type(n) == int) s = str(n) return ((2000>n>1000) and (n == int(s[0]*len(s))))

  2. def rc2(s, t, n): q = "" for i in range(len(s)): q += t[(i+n)%len(t)] print(q) return ((len(s) == 2*len(t)) and (not s.startswith(t)) and (q == s))

  3. import string def rc3(n,k,x): s = "ab\tc\\d" assert(len(s) == n) assert(s[k] in string.whitespace) assert(s.find("b") == s.find("e") + x) return True

  4. def rc4(s): assert (s[0] == "d" and len(s) == 5) for i in range(1, len(s)): if (ord(s[i]) != (i + ord(s[i-1]))): #1 + d return False return True