Skip to content

Commit fa6a908

Browse files
Recursion - Deep reverse array
1 parent 27d70b5 commit fa6a908

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

deep_reverse.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def reverse(l):
2+
out = list()
3+
if len(l) == 0:
4+
return list()
5+
for index in range(len(l),0,-1 ):
6+
if isinstance(l[index-1], list):
7+
to_append = reverse(l[index-1])
8+
else:
9+
to_append = l[index-1]
10+
out.append(to_append)
11+
return out
12+
13+
def test_function(test_case):
14+
arr = test_case[0]
15+
solution = test_case[1]
16+
17+
output = reverse(arr)
18+
if output == solution:
19+
print("Pass")
20+
else:
21+
print("False")
22+
23+
test_case = [[1,2,3], [3,2,1]]
24+
test_function(test_case)
25+
26+
test_case = [[1, 2, [3, 4, 5], 4, 5], [5, 4, [5, 4, 3], 2, 1]]
27+
test_function(test_case)
28+
29+
test_case = [[1, [2, 3, [4, [5, 6]]]], [[[[6, 5], 4], 3, 2], 1]]
30+
test_function(test_case)
31+
32+
test_case = [ [1, [2,3], 4, [5,6]], [ [6,5], 4, [3, 2], 1]]
33+
test_function(test_case)

0 commit comments

Comments
 (0)