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