Q1
python
def multiply(m, n):
"""Takes two positive integers and returns their product using recursion.
>>> multiply(5, 3)
15
"""
"*** YOUR CODE HERE ***"
if m == 1:
return n
else:
return n + multiply(m - 1, n)
Q3
python
def skip_mul(n): # Find the bug
"""Return the product of n * (n - 2) * (n - 4) * ...
>>> skip_mul(5) # 5 * 3 * 1
15
>>> skip_mul(8) # 8 * 6 * 4 * 2
384
"""
if n == 1 or n == 2:
return n
else:
return n * skip_mul(n - 2)
Q4
python
def is_prime(n):
"""Returns True if n is a prime number and False otherwise.
>>> is_prime(2)
True
>>> is_prime(16)
False
>>> is_prime(521)
True
"""
"*** YOUR CODE HERE ***"
'''
num = n + 1
if n == 1:
return True
elif (num - 1) % n == 0:
return False
return is_prime(n - 1)
''' # My original intention is that "If a number N is not prime, then it can be divided into a prime number P and another number M"
i = n - 1
def prime_helper(n, i):
if i == 1:
return True
elif n % i == 0:
return False
return prime_helper(n, i - 1)
return prime_helper(n, i)
Solution
From bottom to top
python
def is_prime(n):
"""Returns True if n is a prime number and False otherwise.
>>> is_prime(2)
True
>>> is_prime(16)
False
>>> is_prime(521)
True
"""
def helper(i):
if i > (n ** 0.5): # Could replace with i == n
return True
elif n % i == 0:
return False
return helper(i + 1)
return helper(2)
Q5
python
def hailstone(n):
"""Print out the hailstone sequence starting at n, and return the number of elements in the sequence.
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
>>> b = hailstone(1)
1
>>> b
1
"""
"*** YOUR CODE HERE ***"
print(n)
times = 0
if n == 1:
return times + 1
elif n % 2 == 0:
return hailstone(n // 2) + 1
else:
return hailstone(n * 3 + 1) + 1
Q6
python
def merge(n1, n2):
"""Merges two numbers by digit in decreasing order.
>>> merge(31, 42)
4321
>>> merge(21, 0)
21
>>> merge (21, 31)
3211
"""
"*** YOUR CODE HERE ***"