Question: Recursion assignment help please: In python for a first year university course. Write recursive functions that solve each of the following problems. Your functions must

Recursion assignment help please:

In python for a first year university course.

Write recursive functions that solve each of the following problems. Your functions must be recursive -- that is, they should have a base case and, in each recursive call, your problem should be reduced to an identical problem of a smaller size. You should have no loops in your code. Your solutions should not be using built-in functions with the exception of len(). Solutions that are not recursive will not be assigned a passing grade.

1. Write a function called "printNums" which prints out the individual digits that exist in the supplied integer. This function does not need to return a value - it only prints out the digits to the console. The output for printNums(2835789) should be:

2 8 3 5 7 8 9 

2. In this question, you will write two functions:

Write a recursive function that counts the number of vowels in a string. Call this function "numVowels".

Write a recursive function that removes the vowels (a,e,i,o,u) from a string containing all lower case letters. Your function should return a new string with the vowels removed. If the list is empty, the resulting string should be empty. You may not use any built-in-functions for this question except for "in" and "len". Call this function "remVowels".

3. Write a recursive function called "multiply" that finds the product of the numbers from n to m (inclusive) by recursively splitting the list in half and finding the product of the first half of the list then the product of the second half of the list and multiplying them together. The following solution, although it solves the problem, is not acceptable as it does not split the list in half repeatedly.

def multiply(n, m): print(n, m) if n > m: return None if n == m: return m return n * multiply(n+1, m) >>>print(multiply(3, 6)) 360 #this is what would be printed 

4. Write a recursive Python function called "swapNeighbours" that accepts a list as a parameter and returns a copy of the list with consecutive pairs of elements swapped. The function should not change the parameter (it should only create a copy). If the list has an odd number of elements, the last element is not swapped and can remain in its original location - the other elements would be swapped.

Example:

>>> nums = [2,4,5,6] >>> swappedLis = swapNeighbours(nums) >>> swappedLis >>> [4,2,6,5] #this is the swapped list.

5. Write a recursive function called "checkLists" that accepts two integer lists as parameters. The function should return True if both of the lists are exactly the same and returns False otherwise. Your function may not use any built-in library functions or the "==" operator to compare equality of the two lists - you can only use the "==" operator to compare two individual integers. Your function must also not use any loops.

>>> checkLists( [4,2,8], [4,2,8] ) >>> True # the lists are equal >>> checkLists( [3,2,9], [3,2,1] ) >>> False # the lists are not equal

Testing Your Recursion Code

Add the following testing function to your code. This should be the only code that runs when we execute your program -- so you will need to add a call to "tester" to make it run. Please note that this function breaks all rules in terms of "user friendliness" -- this has been done to minimize the output for the TAs to aid in marking.

def tester(): printNums(54321) print() printNums(987) print() printNums(4) print() print(numVowels("mnsty")) print(numVowels("")) print(numVowels("annn")) print(numVowels("nnnnna")) print(numVowels("canoe")) print(remVowels("mnsty")) print(remVowels("")) print(remVowels("annn")) print(remVowels("nnnnna")) print(remVowels("canoe")) print(multiply(4, 7)) print(multiply(2, 10)) print(multiply(8, 2)) print(swapNeighbours([2,3,4])) print(swapNeighbours([1])) print(swapNeighbours([4,5,6,7])) print(swapNeighbours([])) print(checkLists([],[])) print(checkLists([2,5,7],[2,5,7])) print(checkLists([7,8,3],[7,8,2])) print(checkLists([2,2],[2,2,2])) print(checkLists([1],[1])) print(checkLists([],[]))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!