-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbrackets.rb
More file actions
44 lines (35 loc) · 691 Bytes
/
Copy pathbrackets.rb
File metadata and controls
44 lines (35 loc) · 691 Bytes
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
def brackets(s)
pairs = {
'{' => '}',
'[' => ']',
'(' => ')'
}
stack = []
s.chars.each do |char|
if pairs.keys.include?(char)
stack << char
else
return 0 if pairs[stack.pop] != char
end
end
return 0 if stack.any?
1
end
require 'minitest/autorun'
class Tests < MiniTest::Unit::TestCase
def test_example_input
assert_equal 1, brackets('{[()()]}')
end
def test_not_properly_nested
assert_equal 0, brackets('([)()]')
end
def test_not_closed
assert_equal 0, brackets('([()]){')
end
def test_empty_string
assert_equal 1, brackets('')
end
def test_not_opened
assert_equal 0, brackets(')()')
end
end