Created
October 15, 2014 15:14
-
-
Save kevinmichaelchen/7c058648cbd8efd60c17 to your computer and use it in GitHub Desktop.
Detect if a string has balanced parens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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