DEV Community

Discussion on: Daily Challenge #101 - Parentheses Generator

Collapse
 
piedcipher profile image
Tirth Patel • Edited

I made it using python.

Edit: Using inbuilt function for generating permutations is a bad idea. This code works fine till n = 5 but then it takes noticeably long long time to execute. For n = 6, it took 1 minute 19 seconds!

from itertools import permutations
for i in set(permutations('()' * int(input()))):
    s = []
    for j in i:
        if j == '(':
            s.append(j)
        elif s != []:
            s.pop()
    if s == []:
        print(''.join(i))
Collapse
 
dwilmer profile image
Daan Wilmer

Quick formatting tip: type python after the three backticks to get python formatting (like '''python but with the correct symbol).

I like how you are using the features Python gives you, even if it took a while for me to get it — after a few minutes I realized that the inner for loop was a check to see if the string i is properly formatted.

Collapse
 
piedcipher profile image
Tirth Patel

Thanks for this 👍 I was looking for something like this before posting but couldn't find anything.