CMU 15-112: Fundamentals of Programming and Computer Science
Quiz7d


Quiz7 Version D (25 minutes)

6 Fill-in-the-Blank, 5 CT, 1 bonusCT

Do not start (and do not look at the problems) until you are instructed to do so!

For any tech fails (laptop or internet stops working, etc.):
Important notes:
  1. Do not start until you are instructed to do so!
  2. We suggest you do not use your browser's zoom feature. Instead...
    • Click near left edge to make font smaller.
    • Click near right edge to make font bigger.
  3. You will have 25 minutes once the proctor says to start.
  4. You will have brief additional time after we stop to scan and submit your solutions.

  5. Just before the quiz...
    1. Have a fully-charged smartphone and laptop, and still plug both in if possible
    2. Log into Gradescope on your phone
    3. Change the screen timeout setting on your phone to never, so your screen doesn't go black if you don't interact with your screen for a while.
      • iPhones: Settings / Display & Brightness / Auto-Lock / Never
      • Android: Settings / Display / Screen timeout / 10 minutes (or the maximum amount of time)
    4. Turn on Do Not Disturb (or otherwise turn off all notifications).
    5. Position your webcam so we can see:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone

  6. During the quiz:
    1. You may not ask questions during the quiz.
      • If you are unsure how to interpret a problem, take your best guess.
    2. You may not touch your laptop or webcam.
      • This includes muting yourself at any point; the proctors may mute you though.
    3. All of these must be visible at all times:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone, with the quiz webpage

  7. After the quiz:
    1. Follow all proctor instructions on how to end the quiz.
    2. Keep everything in view (as noted above) until the proctor calls "time".
    3. When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
    4. After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
    5. Even then, remain in quiz mode until the proctor calls "all clear"


More important notes:
  • For any tech fails, follow instructions at the top of this page
  • Write your answers by hand with no calculators, no computers.
  • No recursion.
  • You may call almostEqual(x, y) and roundHalfUp(d) without writing them. Write everything else!


Fill-in-the-Blank: Case studies [50 points total]

Fill in the blanks with the missing lines of code from some recent case studies in the course notes.
Note: You may call other functions written in the case studies and assume they are written for you. You just need to fill in the blanks.
Fill in *only one line* (or part of a line) for each blank or you will not receive credit.

NOTE: There are 6 blanks! Your first incorrect / lowest answer will be worth 0, and the other 5 worth 10 points each. You do not have to have the exact line and and variable name memorized. If the code runs, you get the points, but it must run. Circle your answers and indicate the number of the blank (1-6) for each. No partial credit, unless we choose to give it for certain very common mistakes.

FitB Part 1 (from playConnect4() case study)
def checkForWin(board, player):
  winningWord = player * 4
  #Remember the point of the case study
  return _____ #1 <-- Fill in this line


FitB Part 2 (from wordSearch(board, word) case study)
def wordSearchFromCellInDirection(board, word, 
                      startRow, startCol, drow, dcol):
  (rows, cols) = (len(board), len(board[0]))
  dirNames = [ ["up-left"  ,   "up", "up-right"],
               ["left"     ,   ""  , "right"   ],
               ["down-left", "down", "down-right" ] ]
  for i in range(len(word)):
    row = startRow + i*drow
    col = startCol + i*dcol
    if ((row < 0) or (row >= rows) or
      (col < 0) or (col >= cols) or
      _____:  #2 <-- Fill in this line
      return None
  #Return the word, starting location, and direction
  return (word, (startRow, startCol), dirNames[drow+1][dcol+1])


FitB Part 3 (from Snake animation)
def takeStep(app):
  (drow, dcol) = app.direction
  (headRow, headCol) = app.snake[0]
  (newRow, newCol) = (headRow+drow, headCol+dcol)
  if ((newRow < 0) or (newRow >= app.rows) or
      (newCol < 0) or (newCol >= app.cols) or
      ((newRow, newCol) in app.snake)):
    app.gameOver = True
  else:
    app.snake.insert(0, (newRow, newCol))
    if (app.foodPosition == (newRow, newCol)):
      placeFood(app)
    else:
      # didn't eat, so...
      _____ #3 <-- Fill in this line


FitB Part 4 (quiz6 dots animation)
Note: You may have solved this problem slightly differently but if you truly understand it (and didn't just memorize a solution) you should be able to infer how to make this code work even if you haven't seen it!

The animation should have the following features:
  1. Each time you click in the white background, you get a new cyan dot of radius 10 centered on the click.
  2. However, if any part of a new dot would overlap an already-drawn cyan dot, then the new dot is not drawn, and instead the mouse click is ignored.
    • Hint: what is the minimum distance between the centers of two non-intersecting circles?
  3. When you click inside a cyan dot, that dot disappears.
  4. Note that app, canvas, and event are abbreviated with a, c, and e.
from cmu_112_graphics import *

def appStarted(app):
  app.dots = _____ #4 <-- Fill in this line

def distance(x1, y1, x2, y2):
  return ((x2 - x1)**2 + (y2 - y1)**2)**0.5

def mousePressed(app, event):
  # first, check if click is inside an existing dot
  for i in range(len(app.dots)):
    cx, cy = app.dots[i]
    if (distance(cx, cy, event.x, event.y) <= 10):
      # we clicked in an existing dot
      _____ #5 <-- Fill in this line
      return

  # second, check if new dot would overlap any existing dots
  for cx,cy in app.dots:
    if (distance(cx, cy, event.x, event.y) <= 20):
      # overlaps another dot, so ignore this mouse press
      return
  app.dots._____ #6 <-- Fill in this line

def redrawAll(app, canvas):
  r = 10
  for cx, cy in app.dots:
    canvas.create_oval(cx-r, cy-r, cx+r, cy+r, fill='cyan')


Did you find all 6 blanks? They're bolded so don't miss any!





2. Code Tracing [50 points total]

What does the following code print?
Be certain to show your work, and also very clearly circle your answer!

NOTE: These CT problems will be autograded. The script will give full credit for correct answers, and partial-credit for just a few particular incorrect answers. However, your lowest CT score on this quiz will be dropped. There are 5 CT scores, the lowest worth 0, and the other 4 worth 12.5 points each.

Be very careful with your brackets here. For full credit, you must have correct brackets around your lists and sublists.

CT 1 (of 5):
def ct1(L, M):
  L.append(M.pop(2))
  M[1].extend(L)
  return M
L = [7]
M = [3, [2], 1]
print(ct1(L, M))





CT 2 (of 5):
def ct2(M):
  rows, cols = len(M), len(M[0])
  for row in range(rows):
    for col in range(1, cols):
      M[row][0] += M[row][col]
  return M
L = [ [ 1, 2, 3 ], [ 2, 5, 7 ] ]
print(ct2(L))





CT 3 (of 5):
def ct3(L):
  while (len(L) > 3):
    x = L.pop(0)
    x += L.pop()
    L.insert(0, x)
L = [ 1, 2, 3, 4, 5, 6 ]
ct3(L)
print(L)





CT 4 (of 5):
import copy

def ct4(L, A, n):
  A[0][0] += n
  A[1] += [10*n]
  L.append(n)

L = [[1], [2]]
C = copy.copy(L)
D = copy.deepcopy(L)
ct4(L, C, 3)
ct4(L, D, 4)
print(L)





CT 5 (of 5):
def ct5(k):
  M = [ ]
  for i in range(2, k):
    M.insert(0, [n*i for n in [1,2,3]])
  return M
print(ct5(4))




4. Bonus Code Tracing [Up to 3 points]

What does the following code print?
Be certain to show your work, and also very clearly circle your answer!

Bonus CT 1 (of 1)
def f(x): return x+1
def g(x): return 10*f(10*x)
def h(t):
  global s
  s = 'g(0)'
  while (eval(s) < 10**t): s = f'g({s})'
  return (chr(ord('A') + str(eval(s)).count('1')))
def bonusCt1():
  print(''.join([h(x) for x in [42, 27, 42]]))
bonusCt1()