File tree 1 file changed +18
-11
lines changed
1 file changed +18
-11
lines changed Original file line number Diff line number Diff line change 1
1
def nesting ( s )
2
- opened = 0
2
+ stack = [ ]
3
3
4
- s . chars . each do |c |
5
- if c == '('
6
- opened += 1
4
+ s . chars . each do |char |
5
+ if char == '('
6
+ stack << char
7
7
else
8
- return 0 if opened == 0
9
- opened -= 1
8
+ return 0 if stack . pop != '('
10
9
end
11
10
end
12
11
13
- opened == 0 ? 1 : 0
12
+ stack . empty? ? 1 : 0
14
13
end
15
14
16
15
require 'minitest/autorun'
@@ -20,11 +19,19 @@ def test_example_input
20
19
assert_equal 1 , nesting ( '(()(())())' )
21
20
end
22
21
23
- def test_unopened
24
- assert_equal 0 , nesting ( '))' )
22
+ def test_example_fail
23
+ assert_equal 0 , nesting ( '( ))' )
25
24
end
26
25
27
- def test_unclosed
28
- assert_equal 0 , nesting ( '((' )
26
+ def test_empty
27
+ assert_equal 1 , nesting ( '' )
28
+ end
29
+
30
+ def test_success
31
+ assert_equal 1 , nesting ( '()()' )
32
+ end
33
+
34
+ def test_not_closed
35
+ assert_equal 0 , nesting ( '(())(' )
29
36
end
30
37
end
You can’t perform that action at this time.
0 commit comments