Fix the one line code that led the error messagein the traceback
| Last Name: |
|
Cornell NetID, all caps: |
|---|
CS 1110 Prelim 1, March 2018
if something:
do something
do more things
|
|---|
|
|
|---|---|
|
|
|
|
| Question | Points | Score |
|---|---|---|
| 1 | 7 | |
| 2 | 15 | |
| 3 | 12 | |
| 4 | 21 | |
| 5 | 8 | |
| 6 | 5 | |
| 7 | 1 | |
| Total: | 69 |
| 1 | ![]() |
|
|---|---|---|
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | ||
| 12 | ||
| 13 | ||
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 |
2. Make the grade! A talented but morally questionable CS 1111 student has written a script make my grade() that takes a list of lab grades, which is a list of ints, and changes them. First, every grade that is 0 is turned into a 5. Then all grades are doubled.
(a) [6 points] Write three conceptually distinct test cases for function make my grade().
Test case #3
Input and expected output:Rationale:
| Last Name: |
|---|
Since you are also talented but morally questionable, you decide to write a helper function to be used by the previous script. It takes two lists (the original grades, original, and the doctored grades, doctored) and an index i.
If the element at index i in doctored is larger than 10, and there are no more than three 10s in doctored, then replace the element at index i in doctored with a 10.
(c) [2 points] Preconditions are often stated because if one violated them, the function would raise an error.
What is one precondition you should add to the specification of the function above? In other words, what condition (if violated) would cause your implementation to raise an error, and what kind of error would that be? (You do not have to give the exact name of the error; just describe it.)
(a) [11 points] What does the Call Stack look like?
The Dutch version of “Happy Birthday” says “Long shall he/she live in glory”. The first half of the sentence is repeated 3 times, then the second half of the song is repeated 3 times. Below on the left is the (error-free) code which prints out this Dutch song. On the right, draw the full call stack as it would look when line 5 has just been executed for the second time. Include crossed-out frames.
| Last Name: |
|---|
(b) [1 point] What went wrong?
Below on the left is code with one or more errors. Below on the right is the error message
| 1 | ||
|---|---|---|
|
||
|
||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
|
||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | ||
| 12 | ||
| 13 | ||
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 | ||
| 19 | ||
| 20 | ||
| 21 | ||
| 22 | ||
| 23 | ||
| 24 |
Page 7
If instring does not contain any c1s, return False
if instring contains 2 or more c1s,
or contains exactly 1 c1 but no c2,
or contains exactly 1 c1 and a c2 before the c1, return the int 0.
| c1 |
|
|||
|---|---|---|---|---|
|
------ | |||
| '(' | ')' | |||
|
'(' | ')' |
|
|
| '(' | ')' | |||
| '(' | ')' | |||
|
'(' | ')' |
|
|
| '(' | ')' | |||
| 'a' | 'b' | |||
def edit(instring, c1, c2):
Page 8
Imagine someone asks us to write a function switch(t1, t2, i1, i2) with the following specification.
|
|---|
Hints: You may wish to draw object diagrams to make sure you understand the setup of the classes and lists involved. Try writing the function to decide whether or not it can be implemented.
def switch(t1, t2, i1, i2):
You are asked to complete a function (on the next page) that creates a payment plan for an
|
The amount owed is divided into a number of installments. |
|---|
>>> payment_plan(1000,2)
Each installment = $500.0
#1: 500.0
#2: 550.0
Total fees charged: $50.0>>> payment_plan(1000,3)
Each installment = $333.33
#1: 333.33
#2: 366.67
#3: 400.0
Total fees charged: $100.0
11
12 The function also prints out the total amount of fees one will have
paid at the end 13 of the payment plan."""
14
15 # STUDENTS: initialize variable installment here with an assignment
statement 16
17
18 print("Each installment = $"+str(round(installment,2)))
19
20 # STUDENTS: Initialize accumulator variable total_fees here with an
assignment 21
22
23
24 # which_payment will have values 0 then 1 then 2...
# DO NOT CHANGE THE FOR-LOOP STRUCTURE GIVEN 25
26 for which_payment in list(range(num_payments)):
27 # STUDENTS: Compute curr_payment and create/update other variables as
appropriate 28
29
30
31
32
33
34 print("#"+str(which_payment+1)+": "+str(round(curr_payment,2)))
35
| 36 |
|---|



