In this programming problem, you are required to write a function named reverse
that return a recursively reversed list of the given list. Note that you should create a new list for the reversed list. In other words, the original list should remain unchanged after calling the reverse
function.
You can start with this template code and ensure your code can run correctly within this template:
def reverse(L):
# your code here
# Note that the following code is for local testing purposes only. You should leave this part of code unchanged and not submit it to the OJ system.
T = int(input())
for t in range(T):
L = eval(input())
rL = reverse(L)
print(L)
print(rL)
Please note that you only have to submit the definition of the reverse
function to the OJ system. The rest of the code is for your local testing purposes only.
Input Specification
The first line of the input gives the number of test cases, , and then test cases follow.
For each test case, it will consist of a single line that contains a list that may recursively contain other lists.
Constraints
Output Specification
For each test case, output two lines. The first line contains the original list. The second line contains the reversed list.
Sample Input
2
[5, 4, 3, 2, 1]
[1, 2, [3, 4, [5, 6]], 7]
Sample Output
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
[1, 2, [3, 4, [5, 6]], 7]
[7, [[6, 5], 4, 3], 2, 1]
Hint
recursion
for-loop
deep-copy
Comments