-
Notifications
You must be signed in to change notification settings - Fork 3
/
Nesting_Structure_Comparison.py
49 lines (38 loc) · 1.79 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
37
38
39
40
41
42
43
44
45
46
47
48
49
# Kata link:
# https://www.codewars.com/kata/520446778469526ec0000001/
# -------------------------------------
# Instructions:
'''
Complete the function/method (depending on the language) to return true/True when its argument is an array that has the same nesting structures and same corresponding length of nested arrays 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 ] )
'''
# -------------------------------------
# Solution:
def same_structure_as(original,other):
if type(original) != list or type(other) != list or len(original) != len(other) or type(original) != type(other):
return False
for x, y in zip(original, other):
if type(x) != type(y) and x not in other:
return False
if type(x) == list and type(y) == list:
return same_structure_as(x,y)
return True
# -------------------------------------
# Basic Tests:
print(same_structure_as([1,[1,1]],[2,[2,2]]) == True and "[1,[1,1]] same as [2,[2,2]]")
print(same_structure_as([1,[1,1]],[[2,2],2]) == False and "[1,[1,1]] not same as [[2,2],2]")
print(same_structure_as([1,'[',']'], ['[',']',1]) == True and "[1,'[',']'] same as ['[',']',1]")
print(" should return True:")
print(same_structure_as([ 1, 1, 1 ], [ 2, 2, 2 ]) )
print(same_structure_as([ 1, [ 1, 1 ] ], [ 2, [ 2, 2 ] ] ))
print(same_structure_as([ [ [ ], [ ] ] ], [ [ [ ], [ ] ] ] ))
print(" should return False:")
print(same_structure_as([ [ [ ], [ ] ] ], [ [ 1, 1 ] ] ))
print(same_structure_as([ 1, [ 1, 1 ] ], [ [ 2, 2 ], 2 ] ))
print(same_structure_as([ 1, [ 1, 1 ] ], [ [ 2 ], 2 ] ))