Skip to content

Instantly share code, notes, and snippets.

@midu
Last active August 29, 2015 14:20
Show Gist options
  • Save midu/fc067781f6c79d83aaf1 to your computer and use it in GitHub Desktop.
Save midu/fc067781f6c79d83aaf1 to your computer and use it in GitHub Desktop.
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