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


Quiz6 Version C


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!


1. Free Response: isIntegerPalindromic(L) [35 points]

We will say that a list L is integer-palindromic, a coined term, if L contains at least one integer, and the integers in L occur in the same order from left-to-right as from right-to-left. For example:
[ 19, 'yes', 2, True, 19, 'amazing']
The integers in this list are [19, 2, 19], and those are the same forwards as backwards, so this list is integer-palindromic.

By contrast:
[ 19, 'no', 2, False, 2, 'sad']
The integers in this list are [19, 2, 2], and those are NOT the same forwards as backwards, so this list is not integer-palindromic.

And finally:
[ 'no', False, 'sad']
This list has no integers, and so it is not integer-palindromic.

With this in mind, write the non-destructive function isIntegerPalindromic(L), which you can abbreviate as iip(L), which takes a possibly-empty list of any values, and returns True if it is integer-palindromic and False otherwise.




2. Free Response: Animation with lists [35 points]

Write appStarted, mousePressed, and redrawAll so that:
  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.
Note that you can abbreviate app, canvas, and event with a, c, and e.




3. Code Tracing [30 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 4 CT scores, the lowest worth 0, and the other 3 worth 10 points each.

CT 1 (of 4):
def ct1(L):
  for i in range(len(L)):
    L[i] *= i
  while L[-1] > L[0]:
    L[0] += L.pop()
  return L

print(ct1([1,3,1,2,1]))





CT 2 (of 4):
def ct2(M):
  L = [ ]
  for v in M:
    if (M[v%len(M)] % v == 0):
      L.insert(0, v)
  return [x**2 for x in L]

print(ct2([4, 8, 10, 18]))





CT 3 (of 4):
def ct3():
  L = [ ]
  M = L
  M += [1]
  M = M + [2]
  return M, L

print(ct3())





CT 4 (of 4):
def ct4(k):
  L = [ k ]
  M = L
  L = L + M
  N = L
  L = M + [2]
  N.extend(M)
  N.append(sum(L))
  return N

print(ct4(3))




4. 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
import math

# helper function for bonusCt1
def f(L):
  return [v**2 for v in L[1:]]

def bonusCt1(q):
  L = list(range(q))
  while L[0] < 9999:
    L = f(L)
  return int(math.log(L[0], 2))

print(bonusCt1(10))

Bonus CT 2 of 2
def bonusCt2(s):
  L = s.splitlines()
  M = s.split()
  s = ''
  for v in L:
    if v in M:
      s += v
  for v in M:
    if v in L:
      s += v
  return s

print(bonusCt2('''
This is a test,
only a test.
A silly, silly
test.
'''))