Skip to content

Instantly share code, notes, and snippets.

@kevinmichaelchen
Created October 15, 2014 15:14
Show Gist options
  • Save kevinmichaelchen/7c058648cbd8efd60c17 to your computer and use it in GitHub Desktop.
Save kevinmichaelchen/7c058648cbd8efd60c17 to your computer and use it in GitHub Desktop.
Detect if a string has balanced parens
#!/usr/bin/ruby
def isBalanced(string)
stack = []
string.split("").each do |i|
if i == '('
stack.push i
elsif i == ')'
popped = stack.pop
return false if popped == nil
else
return false
end
end
stack.empty?
end
if isBalanced(ARGV[0])
puts "Balanced"
else
puts "Not balanced"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment