Generate parentheses solutionPrint the string equivalents of a phone numberAnother permutatorPassword Attacker (Google apac test problem)You need to diversify your stringsCheck for balanced parentheses in JavaScriptTwo approaches to print all permutations - returning versus passing through the “result” listFinding pairs of complementary numbersRead list of dictionaries with nested dictionariesMinimum number of parentheses to be removed to make a string of parentheses balancedReturn all valid paren strings with a given lengthPython program to remove invalid parentheses
Which are the methodologies for interpreting Vedas?
Keeping track of theme when improvising
Fastest way from 8 to 7
I sent an angry e-mail to my interviewers about a conflict at my home institution. Could this affect my application?
Part of my house is inexplicably gone
What do you call the action of "describing events as they happen" like sports anchors do?
Am I being scammed by a sugar daddy?
Idiom for 'person who gets violent when drunk"
Dedicated bike GPS computer over smartphone
What is the theme of analysis?
Why would a car salesman tell me not to get my credit pulled again?
What is Gilligan's full name?
Jam with honey & without pectin has a saucy consistency always
Is the first of the 10 Commandments considered a mitzvah?
Does WiFi affect the quality of images downloaded from the internet?
A life of PhD: is it feasible?
Placement of positioning lights on A320 winglets
ISP is not hashing the password I log in with online. Should I take any action?
Is it true that "only photographers care about noise"?
What's the difference between DHCP and NAT? Are they mutually exclusive?
Is it advisable to add a location heads-up when a scene changes in a novel?
The best in flight meal option for those suffering from reflux
A team managed by my peer is close to melting down
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Generate parentheses solution
Print the string equivalents of a phone numberAnother permutatorPassword Attacker (Google apac test problem)You need to diversify your stringsCheck for balanced parentheses in JavaScriptTwo approaches to print all permutations - returning versus passing through the “result” listFinding pairs of complementary numbersRead list of dictionaries with nested dictionariesMinimum number of parentheses to be removed to make a string of parentheses balancedReturn all valid paren strings with a given lengthPython program to remove invalid parentheses
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have coded a solution to build all valid permutations of parentheses.
My code is below.
I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to include the line return
anywhere in the code (initially I included one). The solution works but I have never had a case using recursion where I didn't have to use the return
line in the base-case.
How come I don't need it here?
The following expression initially had a return
statement but I was told this was obsolete.
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
return
Python 3.7 code:
"""Module builds all valid permutations of n parentheses
For example: n = 3:
((()))
(()())
(())()
()(())
()()()
Total = 5
"""
def build_parentheses(number_pairs: int,
output="",
number_open=0,
number_closed=0)-> str:
"""The function that builds the parentheses. Output as a string:
number_pairs: number of parentheses pairs user desired
All other parameters are private
"""
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
else:
if number_open < number_pairs:
output += "("
build_parentheses(number_pairs, output, number_open + 1, number_closed)
output = output[:-1]
if number_closed < number_open and number_open:
output += ")"
build_parentheses(number_pairs, output, number_open, number_closed + 1)
if __name__ == "__main__":
build_parentheses.counter = 0
build_parentheses(5)
print(f"=========nbuild_parentheses.counter solutions")
By comparison, in this post I made, I did use the return statement.
python recursion combinatorics balanced-delimiters
$endgroup$
|
show 4 more comments
$begingroup$
I have coded a solution to build all valid permutations of parentheses.
My code is below.
I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to include the line return
anywhere in the code (initially I included one). The solution works but I have never had a case using recursion where I didn't have to use the return
line in the base-case.
How come I don't need it here?
The following expression initially had a return
statement but I was told this was obsolete.
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
return
Python 3.7 code:
"""Module builds all valid permutations of n parentheses
For example: n = 3:
((()))
(()())
(())()
()(())
()()()
Total = 5
"""
def build_parentheses(number_pairs: int,
output="",
number_open=0,
number_closed=0)-> str:
"""The function that builds the parentheses. Output as a string:
number_pairs: number of parentheses pairs user desired
All other parameters are private
"""
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
else:
if number_open < number_pairs:
output += "("
build_parentheses(number_pairs, output, number_open + 1, number_closed)
output = output[:-1]
if number_closed < number_open and number_open:
output += ")"
build_parentheses(number_pairs, output, number_open, number_closed + 1)
if __name__ == "__main__":
build_parentheses.counter = 0
build_parentheses(5)
print(f"=========nbuild_parentheses.counter solutions")
By comparison, in this post I made, I did use the return statement.
python recursion combinatorics balanced-delimiters
$endgroup$
$begingroup$
At which line did you originally include thereturn
statement?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
After the linebuild_parentheses.counter += 1
in the base-caseif....
$endgroup$
– EML
8 hours ago
1
$begingroup$
The key difference is you are using anelse:
block here, rendering thereturn
obsolete.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
No, but because all remaining code in the function is in theelse
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago
|
show 4 more comments
$begingroup$
I have coded a solution to build all valid permutations of parentheses.
My code is below.
I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to include the line return
anywhere in the code (initially I included one). The solution works but I have never had a case using recursion where I didn't have to use the return
line in the base-case.
How come I don't need it here?
The following expression initially had a return
statement but I was told this was obsolete.
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
return
Python 3.7 code:
"""Module builds all valid permutations of n parentheses
For example: n = 3:
((()))
(()())
(())()
()(())
()()()
Total = 5
"""
def build_parentheses(number_pairs: int,
output="",
number_open=0,
number_closed=0)-> str:
"""The function that builds the parentheses. Output as a string:
number_pairs: number of parentheses pairs user desired
All other parameters are private
"""
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
else:
if number_open < number_pairs:
output += "("
build_parentheses(number_pairs, output, number_open + 1, number_closed)
output = output[:-1]
if number_closed < number_open and number_open:
output += ")"
build_parentheses(number_pairs, output, number_open, number_closed + 1)
if __name__ == "__main__":
build_parentheses.counter = 0
build_parentheses(5)
print(f"=========nbuild_parentheses.counter solutions")
By comparison, in this post I made, I did use the return statement.
python recursion combinatorics balanced-delimiters
$endgroup$
I have coded a solution to build all valid permutations of parentheses.
My code is below.
I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to include the line return
anywhere in the code (initially I included one). The solution works but I have never had a case using recursion where I didn't have to use the return
line in the base-case.
How come I don't need it here?
The following expression initially had a return
statement but I was told this was obsolete.
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
return
Python 3.7 code:
"""Module builds all valid permutations of n parentheses
For example: n = 3:
((()))
(()())
(())()
()(())
()()()
Total = 5
"""
def build_parentheses(number_pairs: int,
output="",
number_open=0,
number_closed=0)-> str:
"""The function that builds the parentheses. Output as a string:
number_pairs: number of parentheses pairs user desired
All other parameters are private
"""
if number_open == number_pairs and number_closed == number_pairs:
print(output)
build_parentheses.counter += 1
else:
if number_open < number_pairs:
output += "("
build_parentheses(number_pairs, output, number_open + 1, number_closed)
output = output[:-1]
if number_closed < number_open and number_open:
output += ")"
build_parentheses(number_pairs, output, number_open, number_closed + 1)
if __name__ == "__main__":
build_parentheses.counter = 0
build_parentheses(5)
print(f"=========nbuild_parentheses.counter solutions")
By comparison, in this post I made, I did use the return statement.
python recursion combinatorics balanced-delimiters
python recursion combinatorics balanced-delimiters
edited 2 hours ago
200_success
133k20165437
133k20165437
asked 8 hours ago
EMLEML
4819
4819
$begingroup$
At which line did you originally include thereturn
statement?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
After the linebuild_parentheses.counter += 1
in the base-caseif....
$endgroup$
– EML
8 hours ago
1
$begingroup$
The key difference is you are using anelse:
block here, rendering thereturn
obsolete.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
No, but because all remaining code in the function is in theelse
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago
|
show 4 more comments
$begingroup$
At which line did you originally include thereturn
statement?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
After the linebuild_parentheses.counter += 1
in the base-caseif....
$endgroup$
– EML
8 hours ago
1
$begingroup$
The key difference is you are using anelse:
block here, rendering thereturn
obsolete.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
No, but because all remaining code in the function is in theelse
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
At which line did you originally include the
return
statement?$endgroup$
– dfhwze
8 hours ago
$begingroup$
At which line did you originally include the
return
statement?$endgroup$
– dfhwze
8 hours ago
$begingroup$
After the line
build_parentheses.counter += 1
in the base-case if....
$endgroup$
– EML
8 hours ago
$begingroup$
After the line
build_parentheses.counter += 1
in the base-case if....
$endgroup$
– EML
8 hours ago
1
1
$begingroup$
The key difference is you are using an
else:
block here, rendering the return
obsolete.$endgroup$
– dfhwze
8 hours ago
$begingroup$
The key difference is you are using an
else:
block here, rendering the return
obsolete.$endgroup$
– dfhwze
8 hours ago
1
1
$begingroup$
No, but because all remaining code in the function is in the
else
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)$endgroup$
– dfhwze
8 hours ago
$begingroup$
No, but because all remaining code in the function is in the
else
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)$endgroup$
– dfhwze
8 hours ago
1
1
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago
|
show 4 more comments
2 Answers
2
active
oldest
votes
$begingroup$
As mentioned here I'll provide a short answer to summarize what we discussed. A return
statement only impacts code that short-circuits any remaining code that would have been called if omitted.
pseudo code snippets below
The return
statement here skips snippet 2.
if (condition)
// .. snippet 1
return;
// snippet 2
The return
statement here is unnecessary.
if (condition)
// .. snippet 1
return;
else
// snippet 2
$endgroup$
add a comment |
$begingroup$
There is no need for a return statement here because when you reach the end of a function, there is an implicit return.
For example:
1 def exampleFunction():
2 if someCondition:
3 doThis()
4 else:
5 doTheOtherThing()
6
We could put a return statement after the call to doThis()
, but this would make no difference to the execution of the code. When someCondition
is True
, we enter that code block and then call doThis()
. Then, we go to line 6 and implicitly return from the function.
So after executing line 3, we jump to line 6 and implicitly return, so explicitly returning after line 3 makes no difference.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222101%2fgenerate-parentheses-solution%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
As mentioned here I'll provide a short answer to summarize what we discussed. A return
statement only impacts code that short-circuits any remaining code that would have been called if omitted.
pseudo code snippets below
The return
statement here skips snippet 2.
if (condition)
// .. snippet 1
return;
// snippet 2
The return
statement here is unnecessary.
if (condition)
// .. snippet 1
return;
else
// snippet 2
$endgroup$
add a comment |
$begingroup$
As mentioned here I'll provide a short answer to summarize what we discussed. A return
statement only impacts code that short-circuits any remaining code that would have been called if omitted.
pseudo code snippets below
The return
statement here skips snippet 2.
if (condition)
// .. snippet 1
return;
// snippet 2
The return
statement here is unnecessary.
if (condition)
// .. snippet 1
return;
else
// snippet 2
$endgroup$
add a comment |
$begingroup$
As mentioned here I'll provide a short answer to summarize what we discussed. A return
statement only impacts code that short-circuits any remaining code that would have been called if omitted.
pseudo code snippets below
The return
statement here skips snippet 2.
if (condition)
// .. snippet 1
return;
// snippet 2
The return
statement here is unnecessary.
if (condition)
// .. snippet 1
return;
else
// snippet 2
$endgroup$
As mentioned here I'll provide a short answer to summarize what we discussed. A return
statement only impacts code that short-circuits any remaining code that would have been called if omitted.
pseudo code snippets below
The return
statement here skips snippet 2.
if (condition)
// .. snippet 1
return;
// snippet 2
The return
statement here is unnecessary.
if (condition)
// .. snippet 1
return;
else
// snippet 2
edited 7 hours ago
answered 7 hours ago
dfhwzedfhwze
2,252323
2,252323
add a comment |
add a comment |
$begingroup$
There is no need for a return statement here because when you reach the end of a function, there is an implicit return.
For example:
1 def exampleFunction():
2 if someCondition:
3 doThis()
4 else:
5 doTheOtherThing()
6
We could put a return statement after the call to doThis()
, but this would make no difference to the execution of the code. When someCondition
is True
, we enter that code block and then call doThis()
. Then, we go to line 6 and implicitly return from the function.
So after executing line 3, we jump to line 6 and implicitly return, so explicitly returning after line 3 makes no difference.
$endgroup$
add a comment |
$begingroup$
There is no need for a return statement here because when you reach the end of a function, there is an implicit return.
For example:
1 def exampleFunction():
2 if someCondition:
3 doThis()
4 else:
5 doTheOtherThing()
6
We could put a return statement after the call to doThis()
, but this would make no difference to the execution of the code. When someCondition
is True
, we enter that code block and then call doThis()
. Then, we go to line 6 and implicitly return from the function.
So after executing line 3, we jump to line 6 and implicitly return, so explicitly returning after line 3 makes no difference.
$endgroup$
add a comment |
$begingroup$
There is no need for a return statement here because when you reach the end of a function, there is an implicit return.
For example:
1 def exampleFunction():
2 if someCondition:
3 doThis()
4 else:
5 doTheOtherThing()
6
We could put a return statement after the call to doThis()
, but this would make no difference to the execution of the code. When someCondition
is True
, we enter that code block and then call doThis()
. Then, we go to line 6 and implicitly return from the function.
So after executing line 3, we jump to line 6 and implicitly return, so explicitly returning after line 3 makes no difference.
$endgroup$
There is no need for a return statement here because when you reach the end of a function, there is an implicit return.
For example:
1 def exampleFunction():
2 if someCondition:
3 doThis()
4 else:
5 doTheOtherThing()
6
We could put a return statement after the call to doThis()
, but this would make no difference to the execution of the code. When someCondition
is True
, we enter that code block and then call doThis()
. Then, we go to line 6 and implicitly return from the function.
So after executing line 3, we jump to line 6 and implicitly return, so explicitly returning after line 3 makes no difference.
answered 7 hours ago
bagbag
456
456
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222101%2fgenerate-parentheses-solution%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
At which line did you originally include the
return
statement?$endgroup$
– dfhwze
8 hours ago
$begingroup$
After the line
build_parentheses.counter += 1
in the base-caseif....
$endgroup$
– EML
8 hours ago
1
$begingroup$
The key difference is you are using an
else:
block here, rendering thereturn
obsolete.$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
No, but because all remaining code in the function is in the
else
, there is no more reachable code detected. If you have more questions about the scope of code blocks, take it to chat :)$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Let us continue this discussion in chat.
$endgroup$
– dfhwze
8 hours ago