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

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

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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