Question

In: Computer Science

For this problem you must write the functions in a recursive manner (i.e. the function must...

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

Solutions

Expert Solution

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


====================================================================


Related Solutions

PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Can you answer these question using python and recursive functions Write a function that consumes two...
Can you answer these question using python and recursive functions Write a function that consumes two numbers N1 and N2 and prints out all the even numbers between those numbers, using recursion. Using any number of list literals of maximum length 2, represent the data from your Survey List. So you cannot simply make a list of 15 values. Make a comment in your code that explicitly describes how you were able to represent this. Write a recursive function that...
The Problem Below are a series of problems you need to solve using recursive functions. You...
The Problem Below are a series of problems you need to solve using recursive functions. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. Implementation Each recursive function MUST have a wrapper function enclosing it where you will do input/output file processing as well...
In C# Problem 1: Order matters! For this question, you’re going to write two recursive functions,...
In C# Problem 1: Order matters! For this question, you’re going to write two recursive functions, both of which take in only a single string as a parameter; you will not receive credit if you pass in an integer as well. Note: you will need to understand substrings to get the correct solution. The first function should simply print out the string, but do so letter by letter – and recursively. The second function should print out the string in...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT