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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!