In: Computer Science
For this problem you must write the functions in a recursive manner (i.e. the function must call itself) – it is not acceptable to submit an iterative solution to these problems.
A. Complete the recursive function gcd(m, n) that calculate the greatest common denominator of two numbers with the following rules:
# If m = n, it returns n
# If m < n, it returns gcd(m, n-m)
# If m > n, it returns gcd(m-n, n) #
def gcd(m,n): return None # Replace this with your implementation
B. Complete the following function that uses recursion to find and return the max (largest) value in the list u.
# find_max([1, 7, 4, 5] returns 7 # find_ max ([1, 7, 4, 5, 9, 2] returns 9 # def find_max(u):
return None # Replace this with your implementation
C. Complete the following recursive function that returns the zip of two lists u and v of the same length. Zipping the lists should place the first element from each into a new array, followed by the second elements, and so on (see example output).
# zip([1, 2, 3], [4, 5, 6]) returns [1, 4, 2, 5, 3, 6] # def zip(u, v):
return None # Replace this with your implementation
D. Complete the following recursive function that removes all occurrences of the number x from the list nums.
# remove_number(5, [1, 2, 3, 4, 5, 6, 5, 2, 1]) returns [1, 2, 3, 4, 6, 2, 1] # def remove_number(x, nums):
return None # Replace this with your implementation
E. Write a recursive function removeLetter(string, letter) that takes a string and a letter as input, and recursively removes all occurrences of that letter from the string. The function is case sensitive.
Some example test cases are below:
>>> removeLetter("test string", "t") es sring >>> removeLetter("mississipi", "i") mssssp
>>> removeLetter("To be or not to be is a question.", "t")
To be or no o be is a quesion.
In PyCharm
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== def gcd(m, n): if m == n: return m elif m < n: return gcd(m, n - m) else: return gcd(m - n, n) def find_max(u): if len(u) == 1: return u[0] else: max = find_max(u[1:]) if max > u[0]: return max else: return u[0] def zip(u, v): if len(u) == 1: return [u[0], v[0]] sub_zip = zip(u[1:], v[1:]) list = [u[0], v[0]] list.extend(sub_zip) return list def remove_number(x, nums): if len(nums) == 1 and nums[0] == x: return [] elif len(nums) == 1: return nums else: list = remove_number(x, nums[1:]) if nums[0] == x: return list else: list.insert(0, nums[0]) return list def removeLetter(string, letter): if len(string) == 1 and string[0] == letter: return '' elif len(string) == 1: return string else: list = removeLetter(string[1:], letter) if string[0] == letter: return list else: return string[0] + list
====================================================================