Last active
August 29, 2015 14:20
-
-
Save midu/fc067781f6c79d83aaf1 to your computer and use it in GitHub Desktop.
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
def can_close?(n, open, close) | |
open > close | |
end | |
def can_open?(n, open, close) | |
n > (open - close) | |
end | |
def par(res = '', n = 0, open = 0, close = 0) | |
if n == 0 | |
puts res | |
else | |
if can_open?(n, open, close) | |
par(res + '(', n - 1, open + 1, close) | |
end | |
if can_close?(n, open, close) | |
par(res + ')', n - 1, open, close + 1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment