-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnesting_structure_comparison.py
36 lines (28 loc) · 1.05 KB
/
nesting_structure_comparison.py
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
"""
Complete the function/method (depending on the language) to return true/True when its argument is an array that has the
same nesting structure as the first array.
For example:
# should return True
same_structure_as([ 1, 1, 1 ], [ 2, 2, 2 ] )
same_structure_as([ 1, [ 1, 1 ] ], [ 2, [ 2, 2 ] ] )
# should return False
same_structure_as([ 1, [ 1, 1 ] ], [ [ 2, 2 ], 2 ] )
same_structure_as([ 1, [ 1, 1 ] ], [ [ 2 ], 2 ] )
# should return True
same_structure_as([ [ [ ], [ ] ] ], [ [ [ ], [ ] ] ] )
# should return False
same_structure_as([ [ [ ], [ ] ] ], [ [ 1, 1 ] ] )
"""
def same_structure_as(original, other):
if type(original) != type(other):
return False
if len(original) == len(other) == 0:
return True
if len(original) != len(other):
return False
for i in range(len(original)):
if bool(type(original[i]) == list) != bool(type(other[i]) == list):
return False
elif type(original[i]) == type(other[i]) == list:
return same_structure_as(original[i], other[i])
return True