Question: Question 5 This question will be about reversing strings by parts. What we mean by that is that let's say we have a string Nabi.


Question 5 This question will be about reversing strings by parts. What we mean by that is that let's say we have a string "Nabi". If we want to reverse "Nabi" by parts with n=1 we only reverse the first 1..n letters which in this case is only "N" so the string stays the same and the output is "Nabi. When n=3 (think of this as reversing str[:3]) is when things start to get interesting. We need to reverse the string with n=1..n meaning we first carry out the operation for n=1 then n=2 and n=3 in that order. With n=1, we get "Nabi", with n=2 operated on that output we flip the first 2 characters of "Nabi" and get "aNbi, and with n=3 operated on that output we flip the first 3 characters of "aNbi" and get "bNai. Write the code for the following questions with this knowledge. Restriction (for both parts): 1. Do input validation. 2. No loops or map can be used, only recursion. Question 5.2 Write a function that reverts the effects of reversing by parts on the input string that has been done at intervals 1 to n. def undo_recursive_reverse_up_to_n(name, n): Recursively corrects the recursive reverse done on the string up to n characters. Restrictions: You should use recursion. You should do input validation. Parameters: name (str): The string to be corrected n (int): How many times the string has been previously reversed Returns: (str) Corrected string Parameters: name (str): The string to be corrected n (int): How many times the string has been previously reversed Returns: (str) Corrected string >>> undo_recursive_reverse_up_to_n('bNai', 3) 'Nabi! >>> undo_recursive_reverse_up_to_n('mkin', 3) 'klmn' >>> undo_recursive_reverse_up_to_n('nlkm', 4)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
