Question
Write the below function that takes a string and applies the following rules repetitively until there is one character left and returns that character: -
Write the below function that takes a string and applies the following rules repetitively until there is one character left and returns that character:
- If the first two characters are "aa" then they are replaced by "b" - If the first two characters are "ba" then they are replaced by "c" - If the first two characters are "ab" then they are replaced by "c" - If the first two characters are "bb" then they are replaced by "a" - If the first character is "c", then it is replaced by the second character. However, if the second character is also "c" then the first "c" is removed. - For everything else, the first character is removed.
The input string contains only the letters "a", "b" and "c".
For example, the string "aabc" goes through the following transformations: "aabc" -> "bbc" -> "ac" -> "c"
Similarly, the string "bacbc" becomes "bacbc" -> "ccbc" -> "cbc" -> "bbc" -> "ac" -> "c"
NOTE: If you solve this question in a recursive fashion, you get 10 points bonus! The bonus will be applied if and only if you pass all the other tests.
"""
def shorten(s): return # Remove this line if you want to answer this question.
(ptyhon)
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