# Run this function
def check(fn, *input, expected):
result = fn(*input)
if expected != result:
print(
f"Function should return the value {expected}, it is returning the value {result}.")
else:
print(f"Congratulations, the test case passed!")
Dr. Timnit is assigning grades to her students. Help her write a grading function that takes a student's score (out of 100) as input and returns a letter grade. The cutoffs for grades are included below.
Range | Grade |
---|---|
[90-100] | A+ |
[80-89] | A |
[70-79] | B |
[60-69] | C |
[50-59] | D |
[0-49] | F |
Define a helper function named isValidScore
that takes in a score as input and returns a boolean. If the score that is passed into this function is between 0
and 100
(both inclusive) return True otherwise return False.
def isValidScore(s):
# Put your code below this comment
return False
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(isValidScore, 50, expected=True)
check(isValidScore, 101, expected=False)
check(isValidScore, -101, expected=False)
check(isValidScore, 100, expected=True)
check(isValidScore, 0, expected=True)
Define a function named grader
that has one parameter. You can name the parameter score. Within the function you should call the isValidScore
function you defined in 1.1 by passing in the score. If the call to isValidScore
return True your function must return the string Valid Score
. Otherwise it should return the string Invalid Score
.
def grader(score):
# Put your code below this comment
return ''
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(grader, 50, expected='Valid Score')
check(grader, 150, expected='Invalid Score')
check(grader, -10, expected='Invalid Score')
Modify the grader
function you defined in 1.2 so that it returns the letter grade for the score. Use the grade cutoffs written above. If the score is greater than or equal to 90 your function should return an A+, if the score is greater than or equal to 80 your function should return an A and so on. You should still use the isValidFunction
to check for invalid input and it must return the string Invalid Score
for any invalid scores.
def grader(score):
# Put your code below this comment
return ''
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(grader, 90, expected='A+')
check(grader, 56, expected='D')
check(grader, 70, expected='B')
check(grader, -50, expected='Invalid Score')
check(grader, 105, expected='Invalid Score')
check(grader, -10, expected='Invalid Score')
Matric exam results came in recently. The Ministry of Education (MoE) of Ethiopia has set the passing score to be 325 out of 700. The Ministry wants to know what percentage of the students passed the exam. Help them in writing a function that calculates the percentage of students that passed the exam.
Create a list called scores
that contains the score of 10 students. Fill the list with random values between 0 and 700.
# Write your code below this comment
Define a function named countHowManyPassed
that takes in a list containing scores as a first argument and the cutoff for passing scores as a second argument. The function should then return the count of the number of scores in the list that are above or equal to the passing score.
def countHowManyPassed(s, p):
# Put your code below this comment
return 0
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(countHowManyPassed, [320, 333, 243, 134, 677,
644, 531, 544, 450, 120], 325, expected=6)
check(countHowManyPassed, [320, 100, 243, 134, 677,
200, 531, 544, 200, 120], 200, expected=7)
You can calculate the percentage using the formula: (count_of_students_who_passed_the_exam / total_number_of_students) * 100
. Try to calculate the percentage of students that passed the exam when the count of students who passed the exam is 7 and the total number of students is 10 using the above formula.
# Write your code below this comment
Define a function named calculatePercentage
that accepts two arguments. The first argument is a list of students' score out of 700 and the second argument is the cutoff for passing score. Your function should use the countHowManyPassed
function you defined earlier. It should then return the percentage of the students that passed.
def calculatePercentage(s, p):
# Put your code below this comment
return 0
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(calculatePercentage, [320, 333, 243, 134, 677,
644, 531, 544, 450, 120], 325, expected=60.0)
check(calculatePercentage, [320, 100, 243, 134, 677,
200, 531, 544, 200, 120], 200, expected=70.0)
Your CS professor loves Armstrong numbers and asks you to help identify some Armstrong numbers for them. A three digit integer is said to be an Armstrong number if the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371.
Define a helper function named extractDigits
that is going to take in an integer and then returns the individual digits within the integer as a list. For example, if you give it 371 it should return the list [1, 3, 7]
. Your list must be sorted in ascending order. Hint: You can use the following pseudo-code:
def extractDigits(n):
# Put your code below this comment
return []
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(extractDigits, 371, expected=[1, 3, 7])
check(extractDigits, 985, expected=[5, 8, 9])
check(extractDigits, 652, expected=[2, 5, 6])
Define a helper function named sumDigits
that is going to take in a list and it should cube (raise the number by the power 3) and then sum them up. For example, if you're given the list [3, 7, 1]
it should return the result of 3**3 + 7**3 + 1**3 which in this case is 371.
def sumDigits(l):
# Put your code below this comment
return 0
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(sumDigits, [3, 7, 1], expected=371)
check(sumDigits, [4, 2, 3], expected=99)
Define a helper function named isArmstrong
that is going to take in a number and returns True if the number is an Armstrong number otherwise return False. You should use the extractDigits
and sumDigits
functions you defined earlier. You should first call the extractDigits
function and then pass the result of that function call to the sumDigits
function. If the number is an Armstrong number return True otherwise return False.
def isArmstrong(n):
# Put your code below this comment
return False
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(isArmstrong, 371, expected=True)
check(isArmstrong, 372, expected=False)
check(isArmstrong, 153, expected=True)
check(isArmstrong, 407, expected=True)
check(isArmstrong, 370, expected=True)
Define a function named findArmstrongNumbers
that finds all 3 digit armstrong numbers. It should return a list of three digit armstrong numbers.
Make sure to return the list of numbers in order from smallest to largest.
def findArmstronNumbers():
# Put your code below this comment
return []
Ethiopians have an ancient method of multiplying integers using only addition, doubling, and halving. Here is how the method works:
- Take two numbers to be multiplied and write them down at the top of two columns.
- Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together
For example: 17 × 34
Halving the first column:
17 | 34 |
---|---|
8 | |
4 | |
2 | |
1 |
Doubling the second column:
17 | 34 |
---|---|
8 | 68 |
4 | 136 |
2 | 272 |
1 | 544 |
Strike-out rows whose first cell is even:
17 | 34 |
---|---|
8 | |
4 | |
2 | |
1 | 544 |
Sum the remaining numbers in the right-hand column:
17 | 34 |
---|---|
8 | -- |
4 | --- |
2 | --- |
1 | 544 |
Total | 578 |
So 17 multiplied by 34, by the Ethiopian method is 578.
Define a helper function named isEven
that takes in an integer and returns True if the number is even or False if it's odd.
def isEven(n):
# Put your code below this comment
return False
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(isEven, 2, expected=True)
check(isEven, 1, expected=False)
check(isEven, 15, expected=False)
Define another helper function named halve
that takes in an integer and then returns a list with the halved values till it reaches 1. Make sure the list you are returning is sorted from smallest to largest. For example, if you give it the number 17 it should return the list [1, 2, 4, 8, 17]
, and if you give it the number 105 it should return the list [1, 3, 6, 13, 26, 52, 105]
.
def halve(n):
# Put your code below this comment
return []
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(halve, 17, expected=[1, 2, 4, 8, 17])
check(halve, 105, expected=[1, 3, 6, 13, 26, 52, 105])
Define another helper function named doubleIt
that takes in two argument. The first argument should be the number that is going to be doubled, and the second should number should be the target length of the list to be returned.(If the second number is 5, you will double the starting number 4 times.) Your function should return a list with the doubled values. Make sure the list is sorted from smallest to largest. For example, if you pass 34 and 5 to your function it should return the list [34, 68, 136, 272, 544]
.
def doubleIt(num, n):
# Put your code below this line
return []
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(doubleIt, 34, 5, expected=[34, 68, 136, 272, 544])
Define a helper function named sum
that takes in two lists. The two lists must have equal length. You should loop through each corresponding pair of elements in the two lists, and if the element in the first list is not even, then you should add the corresponding element from the second list to your total. Your function should use the isEven
function you defined earlier to check if the element in the first list is even or not.
For example, calling sum([17, 8, 4, 2, 1], [34, 68, 136, 272, 544])
should return 578.
def sum(list1, list2):
# Put your code below this comment
return 0
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(sum, [17, 8, 4, 2, 1], [34, 68, 136, 272, 544], expected=578)
Finally, define a function named multiply
that takes in two arguments (the numbers that are going to be multiplied) and returns the result of multiplying the first number by the second number. You should use the halve
and doubleIt
functions inside this function. Then, pass the results returned from the two helper functions to the sum
function.
def multiply(a, b):
# Put your code below this comment
return 0
# TEST_CASE
# Note: if an error is given saying "check" is not defined, rerun the first code
# cell in this notebook.
check(multiply, 17, 34, expected=578)
check(multiply, 15, 15, expected=225)
check(multiply, 24, 34, expected=816)