Question

In: Computer Science

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.

  1. (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 index [] and slice operators [:] for strings. No other built-in functions may be used.

  2. (10 points) Write a recursive function called occurrences with two parameters: a string astr and another (nonempty) string substr. The function returns the number of times the substring substr appears in the string astr. For example, occurrences("how now brown cow", "ow") should return 4, occurrences("house mouse louse", "ow") should return 0, and occurrences("green eggs and ham", "egg") should return 1. Your implementation should not contain any loops and may use only the index [] and slice operators [:] for strings. No other built-in functions may be used. Please note that in is a built-in function, and you may not use it to implement your function.

3. (10 points) Write a recursive function called duplicates with a single parameter L, a list of items. The function returns True if L has any duplicate (i.e. repeating) items and False otherwise. The function must be implemented recursively. The base case occurs when the list is empty (in which case it returns False). Your function should consist only of the base case and recursive calls in an if/else statement. You are not allowed to use any built-in functions other than len for lists. Please note that in is a built-in function, and you may not use it to implement your function. In addition, you are only allowed to use the index operator [] and the slice operator [:] for lists. There should not be any loops (for or while) in your implementation! Hint: Your function will need two recursive calls, not just one.

Solutions

Expert Solution

ANSWER:

Program 1

# recursive method to remove occurances of character from string

def remove_char(astr, ch):

# if string is empty return

if not astr:

return ""

# check the first char

if astr[0] == ch:

return remove_char(astr[1:], ch)

# add the first character and recur the method

return astr[0] + remove_char(astr[1:], ch)

#call the above method

print(remove_char('object oriented','e'))

Screesnhot

Output

Program 2

# method which returns the number of occurrences of str2 in str1

def occurrences(str1, str2):

# find the length of strings

n1 = len(str1)

n2 = len(str2)

# consition to exit the recursion

if n1 == 0 or n1 < n2:

return 0

# check if the string matches

if str1[: n2] == str2:

return occurrences(str1[n2 - 1:], str2) + 1

return occurrences(str1[n2 - 1:], str2)

#call the above methods

print(occurrences("green eggs and ham","egg"))

Screenshot

Output

Program

#method to check if a ist has duplicates

def duplicates(L):

#if list is empty retusn false

if len(L) <= 1:

return False

#now check first and second elements

if L[0] == L[1]:

return True

#call the method with first element and rest

if duplicates([L[0]] + L[2:]):

return True

if duplicates(L[1:]):

return True

#if we reach the end , retusn False

return False

#call the above method

print(duplicates([1,2,5,3,4,2]))

Screenshot

Output

If you do not get anything in this solution ,please put a comment and i will help you out.

Do not give a downvote instantly.It is a humble request.

If you like my answer,please give an upvote .....Thank you.


Related Solutions

For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
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...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
Write a Python program to implement one studied entropy coding method, including two separate functions for...
Write a Python program to implement one studied entropy coding method, including two separate functions for encoding and decoding. Use the lyrics of your favorite song as the message to be processed, and test the program on the message. Include the source code, and detail the program design and execution procedure in this section.
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
write both non-recursive and recursive functions that take the strings from the user and display strings...
write both non-recursive and recursive functions that take the strings from the user and display strings in backwards in python
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it. quickSort.cpp #include <iostream> using namespace std;    // A helper function to facilitate swapping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT