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;








2












$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.










share|improve this question











$endgroup$











  • $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






  • 1




    $begingroup$
    The key difference is you are using an else: block here, rendering the returnobsolete.
    $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

















2












$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.










share|improve this question











$endgroup$











  • $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






  • 1




    $begingroup$
    The key difference is you are using an else: block here, rendering the returnobsolete.
    $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













2












2








2





$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.










share|improve this question











$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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









200_success

133k20165437




133k20165437










asked 8 hours ago









EMLEML

4819




4819











  • $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






  • 1




    $begingroup$
    The key difference is you are using an else: block here, rendering the returnobsolete.
    $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
















  • $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






  • 1




    $begingroup$
    The key difference is you are using an else: block here, rendering the returnobsolete.
    $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















$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 returnobsolete.
$endgroup$
– dfhwze
8 hours ago




$begingroup$
The key difference is you are using an else: block here, rendering the returnobsolete.
$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










2 Answers
2






active

oldest

votes


















2












$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






share|improve this answer











$endgroup$




















    2












    $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.






    share|improve this answer









    $endgroup$













      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
      );



      );













      draft saved

      draft discarded


















      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









      2












      $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






      share|improve this answer











      $endgroup$

















        2












        $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






        share|improve this answer











        $endgroup$















          2












          2








          2





          $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






          share|improve this answer











          $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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 7 hours ago









          dfhwzedfhwze

          2,252323




          2,252323























              2












              $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.






              share|improve this answer









              $endgroup$

















                2












                $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.






                share|improve this answer









                $endgroup$















                  2












                  2








                  2





                  $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.






                  share|improve this answer









                  $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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 7 hours ago









                  bagbag

                  456




                  456



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單