# Library Function
def check(actual, expected):
if expected != actual:
print(
f"Function should return the value {expected}, it is returning the value {actual}")
else:
print(f"Congratulations, the test case passed!")
Winston is good at math, but if it's too simple, he gets bored 😒. His teacher needs to find a fun problem for Winston, so they ask for your help to write a function that returns a list of factors for a number.
You have to create a function called findFactors that takes a number as an argument and returns all of its factors as the output.
Make sure to use recursion.
# Your code here
def findFactors(num, fact=None, factors=[]):
return factors
# Test case
check(findFactors(1), [1])
Congratulations, the test case passed!
# Test case
check(findFactors(10), [10, 5, 2, 1])
Congratulations, the test case passed!
Write a function called isPrime() that takes one integer as an argument and returns True if the number is prime and False if the number is "Composite" or "Neither a prime nor a composite" based on the number of factors the number has.
Hint: Use findFactors() from your previous problem
def isPrime(num):
return True
# Test case
check(isPrime(1), False)
Congratulations, the test case passed!
# Test case
check(isPrime(10), False)
Congratulations, the test case passed!
You know what? Winston's teacher changed her mind. She now wants only the Prime-Factorization.
The Prime-Factorization of a number $n$ is the list of all prime numbers that divide $n$.
Write a function primeFactorization()
that takes in a number num
and returns its Prime-Factorization
# put your code here
def findPrimeFactors(num, fact=None, factors=[]):
return factors
# Test case
check(findPrimeFactors(1), [])
check(sorted(findPrimeFactors(10)), [2, 5])
Congratulations, the test case passed! Congratulations, the test case passed!
Twin-Primes are prime numbers separated by a distance of 2 (eg. $5$ and $7$, $11$ and $13$).
Write a program to list all the twin-primes from 1-1000.
Imagine yourself in a world where the only numbers that are known are the even numbers. So, in this world, the only numbers that exist are ...-4, -2, 0, 2, 4, ...
We say a number $m$ E-divides a number $n$ if there is an even number $k$ such that $n=mk$.
We say a number $p$ is E-prime if it is not E-divisible by any even numbers.
For example, $4$ is not E-prime because $4=2\times2$. However, $6$ is E-prime, since $2$ DOES NOT E-divide $6$.
The first E-primes are:
$2, 6, 10, 14, 18, 22, 26, 30.$
First, write a function EFactors()
that takes in a number num
, and returns a list of the even-numbers that E-divides num
.
Next, write a function EPrimeFactors()
that takes in a number num
and returns all the E-primes that E-divide num
.
Run EPrimeFactors()
on the number $180$. What do you notice when you multiply all these E-prime factors together? Is it more than $180$?
# put your code here
def eFactor(num, fact=None, factors=[]):
return factors
# Test cases
check(sorted(eFactor(4)), [2])
check(eFactor(10), [])
Congratulations, the test case passed! Congratulations, the test case passed!
# put your code here
def ePrimeFactors(num):
ePrimeFactors = []
# your code
return ePrimeFactors
# Test Cases
check(sorted(ePrimeFactors(12)), [2, 6])
check(sorted(ePrimeFactors(60)), [2, 6, 10, 30])
Congratulations, the test case passed! Congratulations, the test case passed!
Winston is taking the geography exam, but he accidentally remembered all the names of all the places he needs to know backwards.
Help Winston by writing a function called myReverse
to reverse a string using recursion (don't use loops).
myReverse
should take a string as an argument and return the reverse of the string.
# Put your code here
def myReverse(str, reversedStr="", index=1):
return reversedStr
# Test case
check(myReverse("hello"), "olleh")
check(myReverse("hello there"), "ereht olleh")
Congratulations, the test case passed! Congratulations, the test case passed!
Winston realizes the city "Aba" in Nigeria is the same forward and reverse. In other words, it is a palindrome.
Now write a function that checks if a word or phrase is a palindrome. Palindrome is a word, phrase, or sequence that reads the same backwards as forwards, e.g. "madam" or "nurses run".
Your function checkPalindrome()
should take one argument, the word or phrase, and return True
if the string is a palindrome and False
if it's not.
Hint: use myReverse() function from the above problem.
# Put your code here
def checkPalindrome(str):
return True
# Test case
check(checkPalindrome("hello"), False)
check(checkPalindrome("nun"), True)
Congratulations, the test case passed! Congratulations, the test case passed!
Consider the function
$f(n) = \cases{f(n/2) \text{ if }n \text{ is even}\\ f(3n+1) \text{ if }n \text{ is odd and } n\neq 1\\ 1 \text{ if } n=1}$
What are the values of $f(1), f(2), ... , f(100)$? What do you think $f(1000)$ is?
# Put your code here
def f(n):
return 1
# Test Cases
check(f(1), 1)
check(f(100), 1)
Congratulations, the test case passed! Congratulations, the test case passed!
Aku loves taking math and computer science classes. For every programming class he passes in a semester, he is allowed to take two new advanced programming classes the next semester. For each math class he passes, he is allowed to take four additional advanced math classes.
Aku started his first semester taking 1 math class and 1 computer science class. Assuming that Aku never fails a class and always take all the classes offered to him, how many math classes and how many computer science classes will he be taking in his 2nd semester? What about in his 8th semester?
Your function courses() should take the semester as an argument and return the number of math and programming courses Aku can take in that semester. (Use recursion, and do not use an explicit formula.)
Hint: Use tuples to return the number of courses
# Put your code here
def courses(semester):
return 1, 1
# Test cases
check(courses(1), (1, 1))
check(courses(2), (4, 2))
check(courses(3), (16, 4))
Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed!
Aku realizes he does not have enough time to take all the classes he wants. The reason is because classes can conflict in the time, and he cannot take more than one class at any time. However, he wants to take as many classes as possible. He is provided a schedule of classes, sorted by finishing time. (The finishing time increases for subsequent courses in the list.) Each class $i$ has both a start time $s_i$ as well as a finishing time $f_i$.
Help Aku by writing the function mostClasses()
Input: A list of pairs called schedule
. Each pair denotes a class, where the first element of each pair denotes the start time, and the second element denotes the finish time.
Output: A list of indices of classes from schedule, such that none of the classes conflict. This has to be the largest possible schedule, meaning that there is no potential schedule of non-conflicting classes that is larger.
For example, a class that begins at 9 and ends at 11 would conflict with a class that begins at 10 and ends at 12, but it would not conflict with a class that begins at 11 and ends at 12.
You may assume that you will always take the first class in schedule. Then, take the next non-conflicting course available that ends at the earliest time.
Hint: What class would you take to leave the most room for the rest of classes? Why
# Put your code here
def mostClassesHelper(schedule, last_class_so_far):
return []
# 5.2 test (notice the input is sorted by finish times)
testSchedule = [[1, 4], [3, 5], [0, 6], [5, 7], [3, 9], [
5, 9], [6, 10], [8, 11], [8, 12], [2, 14], [12, 16]]
check(mostClasses(testSchedule), [0, 3, 7, 10])
Congratulations, the test case passed!
Getu is buying pencils, books, and other supplies for the National exam.
Getu only has 5-Birr, 11-Birr, and 17-Birr bills.
The seller does not have change and refuses to change her prices. This means that, for example, if an item costs 19-Birr, Getu cannot purchase it. However, if an item is 16-Birr, Getu can buy it (using a 5-Birr and a 11-Birr bill).
Write a function canBuy
that takes in the cost of an item. If Getu can purchase it, return possible. If not, return impossible.
# Put your code here
def isPossible(cost):
return "possible"
# Test cases
check(isPossible(6), "impossible")
check(isPossible(20), "possible")
check(isPossible(-1), "Item cannot cost less than 0.")
Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed!