Quiz10

Quiz10 Version B


2 FRs, 8 SAs, 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...
  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 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.


More important notes:

0. TA-led mini-lectures [10 points]

If you attended the entirety of a TA-led term project mini-lecture, these 10 points are yours. No need to write anything for this. :]



1. Free Response: Big-Oh [25 points]

The following function f(L) runs in O(N**2), where len(L) == N. Write the function g(L) that behaves identically to f(L) only g(L) runs in O(N). Assume that L is a non-empty list of integers.
def f(L):
  total = count = 0
  for i in range(len(L)):
    v = L[i]
    M = L[i+1:]
    if (v not in M):
      total += v
      count += 1
  return total/count




2. Free Response: Objects [25 points]

You are given this code:
  from dataclasses import make_dataclass
  Dot = make_dataclass('Dot', ['position', 'isBlue'])
This defines the Dot class, where a Dot has two values, an (x,y) position and a boolean value isBlue indicating whether or not the dot is blue.
With this in mind, write the function makeCenteredDot(dot1, dot2) that takes two Dot instances and returns a third Dot instance that is positioned at the midpoint between dot1 and dot2, and is blue if and only if dot1 and dot2 have the same color. (Assume dots can only be blue or red.)
Here are some test cases for you:
  dot1 = Dot( position=(0, 0), isBlue=True )
  dot2 = Dot( position=(2, 2), isBlue=False)
  # The new position is at (1, 1), which is the midpoint between
  # (0, 0) and (2, 2).  Also, the new dot is not blue because
  # dot1 and dot2 do not have the same color.
  dot3 = Dot( position=(1, 1), isBlue=False)
  assert(makeCenteredDot(dot1, dot2) == dot3)

  dot1 = Dot( position=(4, 2), isBlue=False )
  dot2 = Dot( position=(0, 0), isBlue=False )
  # The new position is at (2, 1), which is the midpoint between
  # (4, 2) and (0, 0).  Also, the new dot is blue because
  # dot1 and dot2 have the same color.
  dot3 = Dot( position=(2, 1), isBlue=True  )
  assert(makeCenteredDot(dot1, dot2) == dot3)




3. Short Answers [40 points total]

Clearly write your answers next to the problem number for each question, and circle them!


SA1. Arrange the following 6 function families in order from smallest to largest:
    N    N**0.5    2**N   logN    1     N**2

SA2. Say you have a profoundly large list L such that binary search would have to make 81 comparisons to verify that a value v is not in L. Then you do this:
    M = sorted(L * 1000)
  To the nearest integer, how many comparisons would binary search have to make to verify that v is not in M?

SA3. If f(N) runs in O(N**3), and f(2000) takes 2 seconds, how long would f(6000) take?

SA4. If f(2000) takes 2 seconds, and f(6000) takes 7 seconds, what is the most likely function family that f runs in?

SA5. Indicate ALL that are TRUE about mergesort:
A) mergesort runs in O(NlogN)
B) mergesort requires O(N) passes
C) on each pass, mergesort does O(logN) steps
D) mergesort uses more space than selectionsort, since mergesort uses an extra "auxiliary" list and selectionsort does not
E) mergesort is generally much faster than selectionsort
SA6. The following is a snapshot of xSortLab running selectionSort. What are the indexes of the next two values to be swapped?



SA7. The following is a snapshot of xSortLab running mergeSort. What are the indexes of the next two values to be compared?



SA8. Fill in the blank:
  def selectionSort(a):
    n = len(a)
    for startIndex in range(n):
      minIndex = startIndex
      for i in range(startIndex+1, n):
        if (a[i] < a[minIndex]):
          minIndex = i
      _________________________ # <-- HERE





4. Bonus Code Tracing [Up to 2 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 bonusCt(n):
  M = [ ]
  while len(M) < 4:
    M.append(n % 19)
    n += 2
    if (n // M[-1] == 2**M[-1]):
      M.append(sum(M)//2**n + 1)
  M.append(M[0]-sum(M))
  s = 'b'
  d = ord(s)
  for dd in M[2:]+M[:2]:
    d += dd
    s += chr(d)
    if (len(s) == 2):
      s += ' '
  return s + '!!!'
print(bonusCt(18))