'Pick a balance number of odd and even numbers in python
I need help please. I have this program.
col1 = [1,2,3,4,5,6,7,8,10,13,16]
col2 = [2,4,5,6,7,9,11,13,14,15,17,18,19,20,21,22]
col3 = [7,8,10,12,13,14,15,16,17,18,19,23,24,25,27,29,30]
col4 = [9,12,17,18,19,20,22,23,24,25,26,28,30,31,33]
col5 = [20,21,24,25,26,28,30,32,33,34,35]
b1 = [1,3,5,7,8]
b2 = [2,4,6,7,9,10,11,12]
def pickem():
pick1 = random.choice(col1)
pick2 = random.choice( [i for i in col2 if i > pick1] )
pick3 = random.choice( [i for i in col3 if i > pick2] )
pick4 = random.choice( [i for i in col4 if i > pick3] )
pick5 = random.choice( [i for i in col5 if i > pick4] )
bonus1 = random.choice(b1)
bonus2 = random.choice(b2)
return (pick1,pick2,pick3,pick4,pick5,bonus1,bonus2)
print( pickem() )
I would like the picked number to have a sum total of 3 odd numbers. Is there a way I can do that? It is choosing random numbers from the list I provided above but it will check to see how many of them are odd and if there are less than 3 or whatever amount I put it will pick again until there are 3 odd numbers in total, but only in col1 - col5. So though the final number are seven digits long, I only want to the first 5 digits to have a total of 3 odds. Thank you for your help.
Solution 1:[1]
Your code can be drastically simplified if you use a list of lists for both the columns and the bonuses, rather than maintaining a separate variable for each list.
To generate the pick results, we pick a random number from the first list. Then, for every list after that, we slice off any element that is less than the element we picked last. Since the columns are sorted in ascending order, we can use the bisect module to efficiently find where to slice.
Finally, we check whether there's less than 3 odd numbers in our selected numbers. If there is, we repick all the numbers. If not, we move to selecting the bonus numbers.
The generation of the bonus results is similar to what you've done, but I've made it more concise using a list comprehension.
Here is the resulting code:
import bisect
import random
columns = [
[1,2,3,4,5,6,7,8,10,13,16],
[2,4,5,6,7,9,11,13,14,15,17,18,19,20,21,22],
[7,8,10,12,13,14,15,16,17,18,19,23,24,25,27,29,30],
[9,12,17,18,19,20,22,23,24,25,26,28,30,31,33],
[20,21,24,25,26,28,30,32,33,34,35]
]
bonuses = [
[1,3,5,7,8],
[2,4,6,7,9,10,11,12]
]
pick_results = []
while not pick_results:
for column in columns:
if not pick_results:
pick_results.append(random.choice(column))
else:
pick_results.append(
random.choice(column[bisect.bisect(column, pick_results[-1]):])
)
# Change to == 3 if you want there to be exactly 3 odd numbers.
if sum(1 for item in pick_results if item % 2 == 1) < 3:
pick_results = []
bonus_results = [random.choice(lst) for lst in bonuses]
print(tuple(pick_results) + tuple(bonus_results))
Solution 2:[2]
Use the Modulus Operator to check if any given value is even or odd:
def is_even(value):
if value % 2 == 0:
return True
else:
return False
The Modulus Operator returns the positive integer residue of the division a / b. So if b = 2, then Modulus will always return 0 for even numbers as all even values divide by 2 with no residue. All odd numbers return 1.
So by implementing the is_even-check you can call this function with all chosen random values and just count how often the functions returns True.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
| Solution 2 | 2FingerTyper |
