I have two questions regarding to a backtrack method. So I was looking at a function that can generate n
parenthesis in all legal ways.
def gen_par(p, left, right, parens=[]):
if left:
gen_par(p + '(', left - 1, right)
if right > left:
gen_par(p + ')', left, right - 1)
if not right:
parens += p,
return parens
print(gen_par('', 2, 2))
# >>> ['(())', '()()']
I noticed that there is a line parens += p,
and the ,
at the end is doing something very important and I don't understand why.
if I take that ,
off, I will get this:
print(gen_par('', 2, 2))
# >>> ['(', '(', ')', ')', '(', ')', '(', ')']
In a addition, I don't understand why parens=[] has to be written in the parameter, and if I move it out to the body:
def gen_par(p, left, right):
parens = []
if left:
gen_par(p + '(', left - 1, right)
if right > left:
gen_par(p + ')', left, right - 1)
if not right:
parens += p,
return parens
This won't work.
So the two questions will be:
- What is the function of that
,
- Why is parens needs to be in the parameter area?
Thanks,