-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ruby - groupings.txt
31 lines (20 loc) · 1.01 KB
/
Ruby - groupings.txt
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
// check to see if ‘(’s, ‘{’s and ‘[’s are all closed in proper order.
def grpingCheck string
answer, grpHsh, grpArr = [], {"(" => ")", "{" => "}", "[" =>"]" }, []
string.chars.map { |char| if ["(", "{", "["].include? char
grpArr.push(char)
else
grpHsh.values_at(grpArr.pop)[0] == char ? answer << true : answer << false
end
}
answer.include?(false) ? false : true
end
grpingCheck("[{()[])}]")
=> true
grpingCheck(“[[[[(((())))]]]]”)
=> true
grpingCheck(“[{{{{{{[[[[[[((((“)
=> false
grpingCheck(“[(])”)
=> false
// I create an array, and when an opening grouper( ‘(‘, ’{‘ or ’[‘ ) is seen, push it into the hash. When a closing grouper is seen, pop out the last element of that array and it should match (checked by comparing it against the key/value hash. if it does, push ‘true’ into the answer array, if it doesn’t, push false. if the answer array has any ‘false’s in it, then it doesn’t pass the test.