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

Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
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):...
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...
In this module you learned how to implement recursive functions in your C++ programs. For this...
In this module you learned how to implement recursive functions in your C++ programs. For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part...
language python use a functions and write a python of a computer with three power operations:...
language python use a functions and write a python of a computer with three power operations: power by two, power by three, and power by four. input a number and operation (^2,^3,or^4). output: the result of the power operation
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...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number in the list and swaps it with the first number. 2.Ignore the first number and sort the remaining smaller list recursively.
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT