Question
***IN PYTHON PLEASE***You Havent Seen the Last of Me! Complete the removeLast() function, which takes two string arguments. This function returns a new string based
***IN PYTHON PLEASE***You Havent Seen the Last of Me!
Complete the removeLast() function, which takes two string arguments. This function returns a new string based on the first string argument, with the following modification: the last occurrence of each letter from the second string has been removed (if a given letter appears n times in the second string, up to the last n occurrences of that letter will be removed from the first string, depending on the number of times that letter appears in the first string).
Your basic strategy should be to process the second string argument, character by character. For each character, locate its last occurrence within the first string argument (we suggest using Pythons rfind() method, described in the lecture slides). If the character is present, remove the rightmost copy of that character from the first string argument. When you have processed every character in the second string argument, return whatever is left of the first string argument. Please note that:
- characters in the second string are NOT guaranteed to appear in the first string in the same numbers (or at all) - there is NO guarantee regarding the relative lengths of the two strings - removeLast() is CASE-SENSITIVE; it only removes characters from the first string if they appear in the same case as they do in the second string
For example, consider the strings "quicksilver" and "blink": 1. The first letter of "blink" ("b") does not appear in "quicksilver", so we move on 2. The second letter of "blink" ("l") appears in "quicksilver", so we remove the last (and in this case, only) occurrence to get "quicksiver" 3. The third letter of "blink" ("i") appears twice in "quicksiver"; we remove the last (rightmost) instance to get "quicksver" 4. The fourth letter of "blink" ("n") does not appear in "quicksver", so we move on 5. The final letter of "blink" ("k") appears in "quicksver", so we remove it to get "quicsver" as our final answer
Hint: Remember that Python strings are immutable; they cannot be modified directly. In order to remove a character from a string, you will need to construct a new string (most likely using slicing) and assign it to the old string variable, replacing the original. We have provided a stub for a helper function named excise() that you may wish to call from within your removeLast() function. It takes a string and an integer (representing the index of a character to remove) as its arguments. If the index is less than 0 or greater than or equal to the length of the string argument, the function returns the string argument unchanged. Otherwise, it uses slicing to return a new string that contains all of the characters from the beginning of the string up to (but not including) the specified index, followed by all of the characters following the specified index.
Examples:
Function Call Return Value removeLast("starlord", "thor") "sarld" removeLast("graymalkin", "rag") "aymlkin" removeLast("Booster Gold", "Aquaman") "Booster Gold"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started