3
\$\begingroup\$

Playing on leetcode coding problem: "Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses."

I realized that my understanding of Python was not so good. I always thought that: since list are mutable object they are kind of passed by reference; so i always try to make a copy of the list before feeding the function to avoid inappropriate behavour as i did below:

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res = []

        def add_parenthesis(l,o,f):
            if len(l)==2*n:
                res.append("".join(l))
                return 
            if f<o:
                return 
            if o>0:
                lp = l.copy()
                lp.append("(")
                add_parenthesis(lp,o-1,f)
            if f>0:
                lm = l.copy()
                lm.append(")")
                add_parenthesis(lm,o,f-1)  

        add_parenthesis(['('],n-1,n)
        return res
\$\endgroup\$
2
  • \$\begingroup\$ We can't review your friend's code here, nor do we explain how code works - you're expected to understand that if you wrote it! Additionally, your title should summarise the problem being solved (e.g. "Generate valid combinations of parentheses") rather than the coding mechanism you used to achieve it. \$\endgroup\$ Jun 28, 2021 at 12:59
  • 3
    \$\begingroup\$ Ok i will make some changes! \$\endgroup\$
    – curious
    Jun 28, 2021 at 13:27

1 Answer 1

1
\$\begingroup\$

In the future, for purposes for code review, please include an example case to run. This makes it easier for reviewers.

if __name__ == '__main__':
    all_solutions = Solution().generate_parentheses(4)
    print(all_solutions)

Style

  • Your variable names are l, o, f, p, m, lp, lm, and res. Use descriptive names instead.
  • Not all code needs comments, but algorithmic code usually does. Add an explanation of how this works.
  • The Solution class doesn't do anything. Drop it, and just define your function directly.

Data structures

  • It's considered bad style to mutate variables not passed into the function. You're essentially using res as a global variable. Global variables are bad, for a variety of reasons. See if you can rewrite it without a global variable, and choose whichever you like better.
  • Since you output a string at the end, go ahead and just pass around strings the whole time instead of lists. The main advantage of lists would be to avoid copying them, but you're already doing that everywhere.
  • There are a lot of different ways to generate parentheses. You may consider writing a python generator instead, using yield or a generator expression. That way, you don't have to store a result list, which will matter for large values of n.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.