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

Quiz3 Version C



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 20 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 lists, list indexing, or recursion.
  • You may call almostEqual(x, y) and roundHalfUp(d) without writing them. Write everything else!


1. Free Response: specialShift [70 points]

Say that a "Special Shift" (a coined term) is a kind of encoder that takes a string and returns a new string where the first lowercase letter is shifted by 1, the second lowercase letter is shifted by 2, and so on. This shifting is with wraparound, so 'z' shifted by 1 becomes 'a'.

All characters that are not lowercase letters are just included as-is without a shift.

With this in mind, write the function specialShift(s) that takes a possibly-empty string s and returns the Special Shift of s.

For example, to compute specialShift('afmy'):
   * shift 'a' by 1 to get 'b'
   * shift 'f' by 2 to get 'h'
   * shift 'm' by 3 to get 'p'
   * shift 'y' by 4 to get 'c' 
     ^^^(remember to wraparound!)
Thus, specialShift('afmy') returns 'bhpc'.

And for another example, to compute specialShift('e Dz9!'):
   * shift 'e' by 1 to get 'f'
   * do not shift the ' ' (it is not a letter)
   * do not shift the 'D' (it is not lowercase)
   * shift 'z' by 2 to get 'b'
   * do not shift the '9' (it is not a letter)
   * do not shift the '!' (it is not a letter)
Thus, specialShift('e Dz9!') returns 'f Db9!'.



2. Fill-in-the-blank: line pattern [10 points]

Fill in the blank with one line of code so that the following code draws this:
import basic_graphics

def draw(canvas, width, height):
  for x in range(0, width+1, 50):
    _________________ # <-- Fill in this line

basic_graphics.run(width=400, height=200)




3. Fill-in-the-blank: funny clock [10 points]

Fill in each of the two blanks with the rest of each missing line of code so that the following code draws this:
import basic_graphics
import math

def draw(canvas, width, height):
  # First draw a red circle with radius r,
  # centered on the center of the canvas
  r = 60
  cx, cy = width/2, height/2
  canvas.create_oval(cx - r, cy - r, 
                     cx + r, cy + r, 
                     outline='red')
  # Now label the numbers on the circle
  r *= 1.2
  for i in range(10):
    # Compute the angle to to the next number
    angle = math.pi/2 + i * (2 * math.pi / 10)

    # Find the (x,y) coordinate of the ith vertex
    # centered at (cx, cy) at the given angle

    x = ____________  # <-- Fill in this line...

    y = ____________  # <-- AND fill in this line

    canvas.create_text(x, y, text=str(i))

basic_graphics.run(width=400, height=200)




4. Code Tracing [10 points]

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

CT 1 (of 1):
def ct1(s):
  t = ''
  u = t
  n = 0
  s = s[1:]
  for c in s:
    if c.isspace():
      n += 1
    elif c.islower():
      t += c.upper() * n
    elif c.isalpha():
      u += c
  return f'{t}-{u}-{n}'

print(ct1('eFg? \ty Q R'))




5. 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():
  s = string.ascii_letters[2:]
  e = 0
  for c in s:
    for d in s:
      e += ord(c) - ord(d) + 1
  return e//100
print(bonusCt1())

Bonus CT 2 of 2
def bonusCt2(s):
  t = ''
  for i in range(len(s)):
    t += s[i:] + s[i:][::-1]
  for c in s[::-1]:
    t = (t.replace(c+c,'') +
         str(t.count(c) - 1))
  return t
print(bonusCt2('abcd'))