Question
Write a recursive functionmax_to_end(t)that moves the largest element in a non-empty listtto the end of the list. The function must not permanently remove or add
Write a recursive functionmax_to_end(t)that moves the largest element in a non-empty listtto the end of the list. The function must not permanently remove or add elements tot; in other words, every element that was intbefore the function is called must be intafter the function is called. For example, iftis the list:
t = [3, 2, 1, 0]
thenmax_to_end(t)changestso that the3is the last element oftand the other elements oftare0,1, and2in some unspecified order. The sample solution happens to changetto
t = [2, 1, 0, 3]
Your function is allowed to uset.pop()to temporarily remove the last element of the list, and it is allowed to uset.append(x)ort + [x]to add an elementxto the end of the list. Your function should not use any other list methods.
You cannot use functions that sort the list, nor should you implement a recursive sorting function.
You may use thelenfunction to get the length of the list.
An alternative solution is to havemax_to_endcall a recursive helper function that uses an index or indexes. If you use this approach then you should swap elements of the list instead of using list methods to remove and add elements to the list.
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