CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Week 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?
- 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"))
- 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?!?"))
- 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"))
- 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))
- # Graphics-CT #1 # Draw a picture on a piece of paper showing what this draws when it runs. from tkinter import * 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) def runDrawing(width=300, height=300): root = Tk() canvas = Canvas(root, width=width, height=height) canvas.pack() draw(canvas, width, height) root.mainloop() print("bye!") runDrawing(300, 300)
- # Graphics-CT #2 # Draw a picture on a piece of paper showing what this draws when it runs. from tkinter import * 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) def runDrawing(width=300, height=300): root = Tk() canvas = Canvas(root, width=width, height=height) canvas.pack() draw(canvas, width, height) root.mainloop() print("bye!") runDrawing(400, 400)
Reasoning Over Code (ROC)
Find values for the parameters so the functons return True:
- def rc1(n): assert(type(n) == int) s = str(n) return ((2000>n>1000) and (n == int(s[0]*len(s))))
- 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))
- 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
- 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