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

Clash Royale CLAN TAG#URR8PPP
$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)
python random statistics simulation
$endgroup$
add a comment |
$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)
python random statistics simulation
$endgroup$
add a comment |
$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)
python random statistics simulation
$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
python random statistics simulation
edited Jan 28 at 18:31
200_success
129k15153415
129k15153415
asked Jan 28 at 16:13
VengeancosVengeancos
1225
1225
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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$
add a comment |
$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$
add a comment |
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
);
);
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%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
$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$
add a comment |
$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$
add a comment |
$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$
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.
answered Jan 28 at 16:40
AJNeufeldAJNeufeld
5,332419
5,332419
add a comment |
add a comment |
$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$
add a comment |
$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$
add a comment |
$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$
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.
edited Jan 28 at 16:43
answered Jan 28 at 16:29
GraipherGraipher
24.7k53587
24.7k53587
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%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
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