-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdeep reverse.py
More file actions
51 lines (38 loc) · 1.08 KB
/
deep reverse.py
File metadata and controls
51 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Question 9: Deep Reverse
# Define a procedure, deep_reverse, that takes as input a list,
# and returns a new list that is the deep reverse of the input list.
# This means it reverses all the elements in the list, and if any
# of those elements are lists themselves, reverses all the elements
# in the inner list, all the way down.
# Note: The procedure must not change the input list.
# The procedure is_list below is from Homework 6. It returns True if
# p is a list and False if it is not.
def is_list(p):
'''
docstring
'''
return isinstance(p, list)
def deep_reverse(l):
'''
docstring
'''
# l.reverse()
# for e in l:
# if is_list(e):
# deep_reverse(e)
# return l
if l == [] or type(l) == int or type(l) == str:
return l
else:
return deep_reverse(l[1:]) + [deep_reverse(l[0])]
#For example,
p = [1, [2, 3, [4, [5, 6]]]]
print(deep_reverse(p))
#>>> [[[[6, 5], 4], 3, 2], 1]
print(p)
#>>> [1, [2, 3, [4, [5, 6]]]]
q = [1, [2,3], 4, [5,6]]
print(deep_reverse(q))
#>>> [ [6,5], 4, [3, 2], 1]
print(q)
#>>> [1, [2,3], 4, [5,6]]