-
Notifications
You must be signed in to change notification settings - Fork 0
/
matching_brackets.rb
54 lines (42 loc) · 1.22 KB
/
matching_brackets.rb
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
50
51
52
53
54
# matching brackets - a VERY provisional approach
def validate(title)
o_brackets = ["(", "[", "{"]
c_brackets = [")", "]", "}"]
stack = []
last_br = 0
title.each_char do |c|
if stack.size > 0
# pass
end
if o_brackets.include?(c)
stack.push(c)
last_br = stack.size - 1
print stack
end
if c_brackets.include?(c) && stack.size == 0
puts("INV - closing bracket appeared before opening bracket")
return false
end
if c_brackets.include?(c) && o_brackets.index(stack[last_br]) != c_brackets.index(c)
puts("INV - closing bracket of different type appeared first")
return false
end
puts (title.index(c) - last_br)
if c_brackets.include?(c) && title.index(c) - title.index(stack.last) < 2
puts("INV - met empty brackets")
return false
end
if c_brackets.include?(c) && o_brackets.index(stack[last_br]) == c_brackets.index(c)
stack.pop()
last_br -= 1
# puts("br match")
end
end
if stack.size > 0
puts("INV - unmatched bracket left")
return false
end
puts("VALID")
return true
end
validate("j(a)n[]z")