Created
September 13, 2021 19:20
-
-
Save les-peters/e1f6dbfd70628b37f65c9197ba2fb818 to your computer and use it in GitHub Desktop.
Parentheses Pairs
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
question = """ | |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. | |
Example: | |
$ formParens(1) | |
$ ["()"] | |
$ formParens(3) | |
$ ["((()))","(()())","(())()","()(())","()()()"] | |
""" | |
def formParens(n): | |
paren_array_keys = {} | |
paren_array = [] | |
if n == 1: | |
return [ "()" ] | |
if n > 1: | |
lower_set = formParens(n - 1) | |
for set in lower_set: | |
prepend_set = '()' + set | |
append_set = set + '()' | |
encase_set = '(' + set + ')' | |
paren_array_keys[prepend_set] = 1 | |
paren_array_keys[append_set] = 1 | |
paren_array_keys[encase_set] = 1 | |
for key in paren_array_keys.keys(): | |
paren_array.append(key) | |
return paren_array | |
print(formParens(1)) | |
print(formParens(3)) |
Author
les-peters
commented
Sep 20, 2021
via email
Rachel:
Thank you very much ; I had only focused on the requested results and did
not see if it would
work beyond that.
Here is a change that seems to help:
for char in range(0, len(set)):
new_parens = '(' + set[:char] + ')' + set[char:]
paren_array_keys[new_parens] = 1
# prepend_set = '()' + set
# append_set = set + '()'
# encase_set = '(' + set + ')'
# paren_array_keys[prepend_set] = 1
# paren_array_keys[append_set] = 1
# paren_array_keys[encase_set] = 1
Les
…On Mon, Sep 20, 2021 at 8:19 AM Rachel M. Carmena ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi!
Just leaving a comment in case it's useful for you 🙌
I tried a similar approach, however I discarded because I missed some
options.
I think it works for 1, 2 and 3 pairs. However, formParens(4) doesn't
show:
(())(())
formParens(5) doesn't show:
((())(()))
((()))(())
(()())(())
(())((()))
(())(()())
(())(())()
(())()(())
()(())(())
formParens(6) doesn't show:
(((())(())))
(((()))(()))
(((())))(())
((()())(()))
((()()))(())
((())((())))
((())(()()))
((())(())())
((())(()))()
((())()(()))
((())())(())
((()))((()))
((()))(()())
((()))(())()
((()))()(())
(()(())(()))
(()(()))(())
(()()())(())
(()())((()))
(()())(()())
(()())(())()
(()())()(())
(())(((())))
(())((()()))
(())((())())
(())((()))()
(())(()(()))
(())(()()())
(())(()())()
(())(())(())
(())(())()()
(())()((()))
(())()(()())
(())()(())()
(())()()(())
()((())(()))
()((()))(())
()(()())(())
()(())((()))
()(())(()())
()(())(())()
()(())()(())
()()(())(())
Kind regards!
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/e1f6dbfd70628b37f65c9197ba2fb818#gistcomment-3898891>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACJV2UYVZH2T3VLMPRBWUULUC4RFXANCNFSM5EL2OTFQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Thank you very much
Thanks to you!
Here is a change that seems to help:
Yes, you got it! 👏 Congrats!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment