Week 1 - Day 3c Supplementary: Functions
This section contains extra material related to functions
Exercise 1: Even Numbers []
1.1 Write a function is_even with input x, so that is_even checks if x is even. The function
returns True if x is even, and returns False otherwise.
# TODO: Your code here
def is_even(x):
pass
Run the following code cell to check your solution.
assert is_even(2) == True
assert is_even(-2) == True
assert is_even(3) == False
print("Test cases passed. ✅")
Exercise 2: Approximating Pi []
2.1 Write a function pi that approximates π . Use the following approximation of π :
π=4 ( 11 − 13 + 15 − 17 +. . .)
Your function should get an input n and compute the sum of the first n terms in the above
approximation.
Play around the input number see when it converges to π .
# TODO: Your code here
Exercise 3: Prime Numbers []
3.1 Write a function is_prime with input x. The function returns True if x is a prime, returns
False otherwise. Please use a for loop instead of a while loop.
Hint: You can use is_even function you definied above.
# TODO: Your code here
Use the following code cell to check your solution.
assert is_prime(1) == False
assert is_prime(2) == True
assert is_prime(3) == True
assert is_prime(97) == True
assert is_prime(85) == False
assert is_prime(87) == False
print("Test cases passed. ✅")
Exercise 4: Prime Factors []
4.1 Write a function factors that, given an input n, returns a list contains all the prime factors
of n. For example, for n = 20, your function should return [2, 5]. You can use the function
is_prime from the previous question.
# TODO: Your code here
Use the following code cell to check your solution.
assert sorted(factors(20)) ==[2, 5]
assert sorted(factors(8)) == [2]
assert sorted(factors(1)) == []
assert sorted(factors(100)) == [2, 5]
assert sorted(factors(105)) == [3, 5, 7]
print("Test cases passed. ✅")
Exercise 5: Twin Primes []
Define a function twin_primes with no input to print all the pairs of twin primes between 2
and 200 in the form of a nested list.
For example, a pair of twin primes is both of the numbers are prime numbers and they have the
difference of 2. Ex. [5, 7], [11, 13], [29, 31]
The result should be in the form of the following [[..., ...], [..., ...], ....]
Use a for loop and the function is_prime.
# TODO: Your code here
Run the following code cell to check your solution.
assert twin_primes() == [
[3, 5],
[5, 7],
[11, 13],
[17, 19],
[29, 31],
[41, 43],
[59, 61],
[71, 73],
[101, 103],
[107, 109],
[137, 139],
[149, 151],
[179, 181],
[191, 193],
[197, 199]]
print("Test cases passed. ✅")
Exercise 6: Writing Functions to Generate Collections []
6.1 Write a function spell(s) that takes in a string s and returns a list of all the substrings of s
that start from the beginning of the string. This should look like building up s starting from the
first letter. For example:
spell("apple") == ["a", "ap", "app", "appl", "apple"]
# TODO: Your code here
6.2 Write a function length_map(lst) that takes in a list of strings lst and returns a
dictionary that maps every string in the list to its length. For example:
length_map(["addis", "coder", "cat", "algorithm"]) == {"addis": 5,
"coder": 5, "cat": 3, "algorithm": 9}
# TODO: Your code here
Exercise 7: Bubble Sort Preview []
Sorting a list is making the list goes from smallest number to the biggest one.
Bubble sort is a simple sorting procedure that repeatedly steps through the input list element by
element, comparing the current element with the one after it, swapping their values if needed.
Use this algorithm to sort lst = [9, 8, 7, 6, 5, 4, 3, 2, 1]
Hint: There are len(lst) number of passes, and in each pass we need to iterate through the
list to compare every adjcent pair of elements.
# TODO: Your code here
lst = [9, 8, 7, 6, 5, 4, 3, 2, 1]
Congratulations 🎉