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...
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, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
(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...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT