Skip to content

Instantly share code, notes, and snippets.

@brunofrank
Created February 1, 2021 22:05
Show Gist options
  • Save brunofrank/0625507c410fda7b60715d506fd93906 to your computer and use it in GitHub Desktop.
Save brunofrank/0625507c410fda7b60715d506fd93906 to your computer and use it in GitHub Desktop.
Test
TESTE_DATA = [
["(a[0]+b[2c[6]]) {24 + 53}", true],
["f(e(d))", true],
["[()]{}([])", true],
["((b)", false],
["(c]", false],
["{(a[])", false],
["([)]", false],
[")(", false],
["", false],
]
#) ## ["(", "[", "[", "["] ==> [ -> (
OPEN_SYMBOLS = ['(', '{', '[']
CLOSE_SYMBOLS = [')', '}', ']']
def brackward_of(symbol)
OPEN_SYMBOLS[CLOSE_SYMBOLS.index(symbol)]
end
def brakets_closed?(text)
brakets = []
text.split('').each do |char|
brakets << char if OPEN_SYMBOLS.include?(char)
if CLOSE_SYMBOLS.include?(char)
open_equivalent = brackward_of(char)
if brakets[brakets.length - 1] == open_equivalent
brakets.slice!(-1)
else
return false
end
end
end
return false if !brakets.empty? || text.empty?
true
end
TESTE_DATA.each do |test|
puts brakets_closed?(test[0]) == test[1] ? "CORRECT" : "FAIL"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment