Counting consecutive selections of a given number when sampling integers in a range

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5












$begingroup$


This script counts consecutive number strikes of a number that you determine in a number-sequence that you also determine.



I would be very pleased if this script can be reviewed in terms of compactness, readability and quality. If this is perfect, please also let me know. But I am shure it will not be.



The script can be tested here



import random

desired_number = int(input("Desired number: "))
lower_range = int(input("Lower range: "))
upper_range = int(input("Upper range: "))
iterations = int(input("Iterations: "))

consecutive_strikes = 0
biggest_strike = 0

for i in range(iterations):
actual_number = random.randint(lower_range, upper_range)
print("Actual number: ", actual_number)

if actual_number == desired_number:
consecutive_strikes += 1
else:
if (consecutive_strikes > biggest_strike):
biggest_strike = consecutive_strikes
consecutive_strikes = 0

# if all numbers are the desired number, the second if-statement in the for-loop
# would never be executed
if (consecutive_strikes > biggest_strike):
biggest_strike = consecutive_strikes

print("Biggest consecutive strike: ", biggest_strike)









share|improve this question











$endgroup$
















    5












    $begingroup$


    This script counts consecutive number strikes of a number that you determine in a number-sequence that you also determine.



    I would be very pleased if this script can be reviewed in terms of compactness, readability and quality. If this is perfect, please also let me know. But I am shure it will not be.



    The script can be tested here



    import random

    desired_number = int(input("Desired number: "))
    lower_range = int(input("Lower range: "))
    upper_range = int(input("Upper range: "))
    iterations = int(input("Iterations: "))

    consecutive_strikes = 0
    biggest_strike = 0

    for i in range(iterations):
    actual_number = random.randint(lower_range, upper_range)
    print("Actual number: ", actual_number)

    if actual_number == desired_number:
    consecutive_strikes += 1
    else:
    if (consecutive_strikes > biggest_strike):
    biggest_strike = consecutive_strikes
    consecutive_strikes = 0

    # if all numbers are the desired number, the second if-statement in the for-loop
    # would never be executed
    if (consecutive_strikes > biggest_strike):
    biggest_strike = consecutive_strikes

    print("Biggest consecutive strike: ", biggest_strike)









    share|improve this question











    $endgroup$














      5












      5








      5





      $begingroup$


      This script counts consecutive number strikes of a number that you determine in a number-sequence that you also determine.



      I would be very pleased if this script can be reviewed in terms of compactness, readability and quality. If this is perfect, please also let me know. But I am shure it will not be.



      The script can be tested here



      import random

      desired_number = int(input("Desired number: "))
      lower_range = int(input("Lower range: "))
      upper_range = int(input("Upper range: "))
      iterations = int(input("Iterations: "))

      consecutive_strikes = 0
      biggest_strike = 0

      for i in range(iterations):
      actual_number = random.randint(lower_range, upper_range)
      print("Actual number: ", actual_number)

      if actual_number == desired_number:
      consecutive_strikes += 1
      else:
      if (consecutive_strikes > biggest_strike):
      biggest_strike = consecutive_strikes
      consecutive_strikes = 0

      # if all numbers are the desired number, the second if-statement in the for-loop
      # would never be executed
      if (consecutive_strikes > biggest_strike):
      biggest_strike = consecutive_strikes

      print("Biggest consecutive strike: ", biggest_strike)









      share|improve this question











      $endgroup$




      This script counts consecutive number strikes of a number that you determine in a number-sequence that you also determine.



      I would be very pleased if this script can be reviewed in terms of compactness, readability and quality. If this is perfect, please also let me know. But I am shure it will not be.



      The script can be tested here



      import random

      desired_number = int(input("Desired number: "))
      lower_range = int(input("Lower range: "))
      upper_range = int(input("Upper range: "))
      iterations = int(input("Iterations: "))

      consecutive_strikes = 0
      biggest_strike = 0

      for i in range(iterations):
      actual_number = random.randint(lower_range, upper_range)
      print("Actual number: ", actual_number)

      if actual_number == desired_number:
      consecutive_strikes += 1
      else:
      if (consecutive_strikes > biggest_strike):
      biggest_strike = consecutive_strikes
      consecutive_strikes = 0

      # if all numbers are the desired number, the second if-statement in the for-loop
      # would never be executed
      if (consecutive_strikes > biggest_strike):
      biggest_strike = consecutive_strikes

      print("Biggest consecutive strike: ", biggest_strike)






      python random statistics simulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 28 at 18:31









      200_success

      129k15153415




      129k15153415










      asked Jan 28 at 16:13









      VengeancosVengeancos

      1225




      1225




















          2 Answers
          2






          active

          oldest

          votes


















          7












          $begingroup$

          When you never need to use the loop index, you should name it _ instead of i.



          for _ in range(iterations):


          If statements don’t need parentheses around the entire condition. You did it properly for the first if, but the last two you added extra parentheses.



          You can use the max function to simplify the tracking of the biggest_strike:



          biggest_strike = max(biggest_strike, consecutive_strikes)


          replacing two lines of code with one. And you get to do that twice.






          share|improve this answer









          $endgroup$




















            8












            $begingroup$

            You can use the itertools for this.



            Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:



            from itertools import groupby

            def longest_streak(values, desired_value):
            return max(len(list(group))
            for value, group in groupby(values)
            if value == desired_value)


            (I think it should be streak, 4. b: "an uninterrupted series" and not strike.)



            Then your main can code become this:



            from random import randint

            if __name__ == "__main__":
            desired_number = int(input("Desired number: "))
            lower_range = int(input("Lower range: "))
            upper_range = int(input("Upper range: "))
            iterations = int(input("Iterations: "))

            numbers = (randint(lower_range, upper_range) for _ in range(iterations))
            print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


            Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory).



            if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.






            share|improve this answer











            $endgroup$












              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              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%2f212394%2fcounting-consecutive-selections-of-a-given-number-when-sampling-integers-in-a-ra%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









              7












              $begingroup$

              When you never need to use the loop index, you should name it _ instead of i.



              for _ in range(iterations):


              If statements don’t need parentheses around the entire condition. You did it properly for the first if, but the last two you added extra parentheses.



              You can use the max function to simplify the tracking of the biggest_strike:



              biggest_strike = max(biggest_strike, consecutive_strikes)


              replacing two lines of code with one. And you get to do that twice.






              share|improve this answer









              $endgroup$

















                7












                $begingroup$

                When you never need to use the loop index, you should name it _ instead of i.



                for _ in range(iterations):


                If statements don’t need parentheses around the entire condition. You did it properly for the first if, but the last two you added extra parentheses.



                You can use the max function to simplify the tracking of the biggest_strike:



                biggest_strike = max(biggest_strike, consecutive_strikes)


                replacing two lines of code with one. And you get to do that twice.






                share|improve this answer









                $endgroup$















                  7












                  7








                  7





                  $begingroup$

                  When you never need to use the loop index, you should name it _ instead of i.



                  for _ in range(iterations):


                  If statements don’t need parentheses around the entire condition. You did it properly for the first if, but the last two you added extra parentheses.



                  You can use the max function to simplify the tracking of the biggest_strike:



                  biggest_strike = max(biggest_strike, consecutive_strikes)


                  replacing two lines of code with one. And you get to do that twice.






                  share|improve this answer









                  $endgroup$



                  When you never need to use the loop index, you should name it _ instead of i.



                  for _ in range(iterations):


                  If statements don’t need parentheses around the entire condition. You did it properly for the first if, but the last two you added extra parentheses.



                  You can use the max function to simplify the tracking of the biggest_strike:



                  biggest_strike = max(biggest_strike, consecutive_strikes)


                  replacing two lines of code with one. And you get to do that twice.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 28 at 16:40









                  AJNeufeldAJNeufeld

                  5,332419




                  5,332419























                      8












                      $begingroup$

                      You can use the itertools for this.



                      Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:



                      from itertools import groupby

                      def longest_streak(values, desired_value):
                      return max(len(list(group))
                      for value, group in groupby(values)
                      if value == desired_value)


                      (I think it should be streak, 4. b: "an uninterrupted series" and not strike.)



                      Then your main can code become this:



                      from random import randint

                      if __name__ == "__main__":
                      desired_number = int(input("Desired number: "))
                      lower_range = int(input("Lower range: "))
                      upper_range = int(input("Upper range: "))
                      iterations = int(input("Iterations: "))

                      numbers = (randint(lower_range, upper_range) for _ in range(iterations))
                      print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


                      Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory).



                      if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.






                      share|improve this answer











                      $endgroup$

















                        8












                        $begingroup$

                        You can use the itertools for this.



                        Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:



                        from itertools import groupby

                        def longest_streak(values, desired_value):
                        return max(len(list(group))
                        for value, group in groupby(values)
                        if value == desired_value)


                        (I think it should be streak, 4. b: "an uninterrupted series" and not strike.)



                        Then your main can code become this:



                        from random import randint

                        if __name__ == "__main__":
                        desired_number = int(input("Desired number: "))
                        lower_range = int(input("Lower range: "))
                        upper_range = int(input("Upper range: "))
                        iterations = int(input("Iterations: "))

                        numbers = (randint(lower_range, upper_range) for _ in range(iterations))
                        print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


                        Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory).



                        if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.






                        share|improve this answer











                        $endgroup$















                          8












                          8








                          8





                          $begingroup$

                          You can use the itertools for this.



                          Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:



                          from itertools import groupby

                          def longest_streak(values, desired_value):
                          return max(len(list(group))
                          for value, group in groupby(values)
                          if value == desired_value)


                          (I think it should be streak, 4. b: "an uninterrupted series" and not strike.)



                          Then your main can code become this:



                          from random import randint

                          if __name__ == "__main__":
                          desired_number = int(input("Desired number: "))
                          lower_range = int(input("Lower range: "))
                          upper_range = int(input("Upper range: "))
                          iterations = int(input("Iterations: "))

                          numbers = (randint(lower_range, upper_range) for _ in range(iterations))
                          print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


                          Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory).



                          if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.






                          share|improve this answer











                          $endgroup$



                          You can use the itertools for this.



                          Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:



                          from itertools import groupby

                          def longest_streak(values, desired_value):
                          return max(len(list(group))
                          for value, group in groupby(values)
                          if value == desired_value)


                          (I think it should be streak, 4. b: "an uninterrupted series" and not strike.)



                          Then your main can code become this:



                          from random import randint

                          if __name__ == "__main__":
                          desired_number = int(input("Desired number: "))
                          lower_range = int(input("Lower range: "))
                          upper_range = int(input("Upper range: "))
                          iterations = int(input("Iterations: "))

                          numbers = (randint(lower_range, upper_range) for _ in range(iterations))
                          print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


                          Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory).



                          if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 28 at 16:43

























                          answered Jan 28 at 16:29









                          GraipherGraipher

                          24.7k53587




                          24.7k53587



























                              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%2f212394%2fcounting-consecutive-selections-of-a-given-number-when-sampling-integers-in-a-ra%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

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)