Skip to content

Commit 9311c92

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent b718a11 commit 9311c92

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""Kata - Nesting Structure Comparison
2+
3+
completed at: 2024-09-24 14:35:36
4+
by: Jakub Červinka
5+
6+
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.
7+
8+
For example:
9+
10+
```javascript
11+
// should return true
12+
[ 1, 1, 1 ].sameStructureAs( [ 2, 2, 2 ] );
13+
[ 1, [ 1, 1 ] ].sameStructureAs( [ 2, [ 2, 2 ] ] );
14+
15+
// should return false
16+
[ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2, 2 ], 2 ] );
17+
[ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2 ], 2 ] );
18+
19+
// should return true
20+
[ [ [ ], [ ] ] ].sameStructureAs( [ [ [ ], [ ] ] ] );
21+
22+
// should return false
23+
[ [ [ ], [ ] ] ].sameStructureAs( [ [ 1, 1 ] ] );
24+
```
25+
```php
26+
same_structure_as([1, 1, 1], [2, 2, 2]); // => true
27+
same_structure_as([1, [1, 1]], [2, [2, 2]]); // => true
28+
same_structure_as([1, [1, 1]], [[2, 2], 2]); // => false
29+
same_structure_as([1, [1, 1]], [[2], 2]); // => false
30+
same_structure_as([[[], []]], [[[], []]]); // => true
31+
same_structure_as([[[], []]], [[1, 1]]); // => false
32+
```
33+
```ruby
34+
# should return true
35+
[ 1, 1, 1 ].same_structure_as( [ 2, 2, 2 ] )
36+
[ 1, [ 1, 1 ] ].same_structure_as( [ 2, [ 2, 2 ] ] )
37+
38+
# should return false
39+
[ 1, [ 1, 1 ] ].same_structure_as( [ [ 2, 2 ], 2 ] )
40+
[ 1, [ 1, 1 ] ].same_structure_as( [ [ 2 ], 2 ] )
41+
42+
# should return true
43+
[ [ [ ], [ ] ] ].same_structure_as( [ [ [ ], [ ] ] ] );
44+
45+
# should return false
46+
[ [ [ ], [ ] ] ].same_structure_as( [ [ 1, 1 ] ] )
47+
```
48+
```python
49+
# should return True
50+
same_structure_as([ 1, 1, 1 ], [ 2, 2, 2 ] )
51+
same_structure_as([ 1, [ 1, 1 ] ], [ 2, [ 2, 2 ] ] )
52+
53+
# should return False
54+
same_structure_as([ 1, [ 1, 1 ] ], [ [ 2, 2 ], 2 ] )
55+
same_structure_as([ 1, [ 1, 1 ] ], [ [ 2 ], 2 ] )
56+
57+
# should return True
58+
same_structure_as([ [ [ ], [ ] ] ], [ [ [ ], [ ] ] ] )
59+
60+
# should return False
61+
same_structure_as([ [ [ ], [ ] ] ], [ [ 1, 1 ] ] )
62+
```
63+
64+
~~~if:javascript
65+
For your convenience, there is already a function 'isArray(o)' declared and defined that returns true if its argument is an array, false otherwise.
66+
~~~
67+
68+
~~~if:php
69+
You may assume that all arrays passed in will be non-associative.
70+
~~~
71+
"""
72+
73+
import ast
74+
75+
class StructureVisitor(ast.NodeVisitor):
76+
def __init__(self):
77+
self.structure = []
78+
79+
def visit_List(self, node):
80+
"""special method called when visiting list"""
81+
self.structure.append('list')
82+
for elem in node.elts:
83+
self.visit(elem)
84+
self.structure.append('end_list')
85+
86+
def visit_Constant(self, node):
87+
self.structure.append('const')
88+
89+
def get_structure(expr):
90+
tree = ast.parse(str(expr), mode='eval')
91+
visitor = StructureVisitor()
92+
visitor.visit(tree)
93+
return visitor.structure
94+
95+
def same_structure_as(original, other):
96+
structure1 = get_structure(original)
97+
structure2 = get_structure(other)
98+
print(structure1, structure2)
99+
return structure1 == structure2
100+

0 commit comments

Comments
 (0)