Question

Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...

Solving Problems Using Recursion (Python):

To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables.

3. Create a function that takes to strings and removes the similar letters from the longer one once.

Ex.

Input = "at" "radiator"

Output = "rdiaor"

notice how only the leftmost a was removed by the function

Homework Answers

Answer #1
def remove_string(a, b):            #functin definition
    if len(a) == 0:         #termination condition when length of shorter one becomes zero
        return b            #retruning string after removing shorter one
    else:
        if a[0] in b:       #if letter is present in longer string
            b = b.replace(a[0], '', 1)
            return remove_string(a[1:], b)          #recursive call
        else:               #if letter is not present in the string
            return remove_string(a[1:], b)          #recursive call

str1, str2 = input('Enter the two strings(space separated): ').split()      #taking user input
if len(str1) > len(str2):       #checking for longer among two
    print(remove_string(str2, str1))        #calling the function
else:
    print(remove_string(str1, str2))

Screenshot of output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
this assignment is about solving problems using recursion. For each question, write a recursive function that...
this assignment is about solving problems using recursion. For each question, write a recursive function that solves the problem asked in that question. Your solutions don't have to be efficient, but they do have to use recursion in a non-trivial way. Put all your code in a file called a9.py. Write a recursive function is_palindrome(s) that returns True or False depending on whether s is a palindrome. This is equivalent to returning s == s[::-1]. Write a recursive function find_min(a)...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
I tried using the modulo to skip the chars but I doesnt work in some cases....
I tried using the modulo to skip the chars but I doesnt work in some cases. There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include #include // print...
Using python, write the program below. Program Specifications: You are to write the source code and...
Using python, write the program below. Program Specifications: You are to write the source code and design tool for the following specification: A student enters an assignment score (as a floating-point value). The assignment score must be between 0 and 100. The program will enforce the domain of an assignment score. Once the score has been validated, the program will display the score followed by the appropriate letter grade (assume a 10-point grading scale). The score will be displayed as...
he final function of this problem does not require the use of either recursion or a...
he final function of this problem does not require the use of either recursion or a list comprension. Rather, it will allow you to practice using conditional logic and variable assignment to gradually build up a return value. Write a function price_string(cents) that takes as input a positive integer cents representing a price given in cents, and that constructs and returns a string in which the price is expressed as a combination of dollars and cents. In general, the format...
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. 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. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
Python Programming Assignment 3 For this assignment you have been given a starting file to use...
Python Programming Assignment 3 For this assignment you have been given a starting file to use for the assignment. Some code is already in this file, and it provides functionality you’ll be using to complete this assignment. Your code needs to go in a specific place, marked #### Your Code Here You code must go under this comment, and you should not modify the other parts of the file, except to add a block comment at the top with your...