CMU 15-112: Fundamentals of Programming and Computer Science
Quiz3d
Quiz3 Version D
Important notes:
- Do not start until you are instructed to do so!
- 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.
- You will have 20 minutes once the proctor says to start.
- You will have brief additional time after we stop to scan and submit your solutions.
- Just before the quiz...
- Have a fully-charged smartphone and laptop, and still plug both in if possible
- Log into Gradescope on your phone
- 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)
- Turn on Do Not Disturb (or otherwise turn off all notifications).
- 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
- During the quiz:
- You may not ask questions during the exam.
- If you are unsure how to interpret a problem, take your best guess.
- You may not touch your laptop or webcam.
- This includes muting yourself at any point; the proctors may mute you though.
- 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
- For any tech fails (laptop or internet stops working, etc.):
- Stop taking the quiz
- Fill out this Google Form immediately
- We will email you soon to set up a 1-on-1 oral quiz with the course faculty
- You may not ask questions during the exam.
- After the quiz:
- Follow all proctor instructions on how to end the quiz.
- Keep everything in view (as noted above) until the proctor calls "time".
- When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
- After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
- 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('pQr!\t eF G'))
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) + 4 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) - 2)) return t print(bonusCt2('abcd'))