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

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 15 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 exam.
      • 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
    4. For any tech fails (laptop or internet stops working, etc.):
      1. Stop taking the quiz
      2. Fill out this Google Form immediately
      3. We will email you soon to set up a 1-on-1 oral quiz with the course faculty

  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:
  • Write your answers by hand with no calculators, no computers.
  • No strings, lists, string or list indexing, or recursion.
  • You may call almostEqual(x, y) and roundHalfUp(d) without writing them. Write everything else!


1. Free Response [70 points]

We will say that a number is "summish" (a coined term) if it is an integer >= 1000 where each digit (after the first two digits on the left) is the sum of the previous two digits to its left.

For example, 3257 is summish because 7==2+5 and 5==3+2.

Also, 112358 is summish because 8==3+5, 5==2+3, 3==1+2, and 2==1+1.

Here are the first several summish numbers:
1011, 1123, 1235, 1347, 1459, 2022, 2134, 2246,...

With this in mind, write the function nthSummish(n) that takes a non-negative integer n and returns the nth Summish number. Thus, nthSummish(0) returns 1011.

2. Code Tracing [30 points; 15 points each]

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

CT 1 of 2:
def ct1(x, y):
  while (x > y):
    x -= y
    y -= 1
  r = 0
  for z in range(y):
    r += z
  return 100*x + r

print(ct1(20, 7))

CT 2 of 2:
def ct2(a, b, c):
  r = 0
  for x in range(a, b, c):
    for y in range(x):
      r = 10*r + y
  return r

print(ct2(0, 5, 2))


3. Bonus Code Tracing [Up to 4 points; 2 points each]

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

Bonus CT 1 of 2
def bonusCt1(n):
  def f(n):
    r,m = 0,n
    while m>0:
      r,m = r+m,m-1
    return 2*r-n
  def g(n):
    return (f(n+1) -
            f(n-1))
  return f(g(n))

print(bonusCt1(10))

Bonus CT 2 of 2
def bonusCt2(n):
  r = 0
  for x in range(2**n):
    while (x > 0):
      r += x%2
      x //= 2
  return r

print(bonusCt2(10))