- Dictionaries make only sense if you have a key and store a value by it. Accessing the value of a key and checking if a key is part of a dictionary is fast (
O(1)
) - iterating all values of a dict
is as slow as a list
- but creating the dictionary is slower then creating a list
and occupies more of your computers memory.
- Using
set
operations avoid using if's
cascades - for an example see further below.
Faster input in a loop can be done using split()
and list decomposing *a,b=[1,2,3,4]
:
for _ in range(int(input("How many tickets do you want to enter? "))):
*nums, power = map(int,
input( ("Input space seperated numbers of your ticket,"
" last one is Powerball: ") ).strip().split())
print(nums, power)
Output:
How many tickets do you want to enter? 3
Input space seperated numbers of your ticket, last one is Powerball: 1 2 3 4 99
[1, 2, 3, 4] 99
Input space seperated numbers of your ticket, last one is Powerball: 2 3 4 7 199
[2, 3, 4, 7] 199
Input space seperated numbers of your ticket, last one is Powerball: 4 2 4 5 6
[4, 2, 4, 5] 6
(Although some more checking might be appropirate for Users that do not enter numebrs at all or too few/out of range ones: Asking the user for input until they give a valid response )
For lotto comparisons a list of "tickets" with a set() of numbers each can be fastly checked correct numbers using set
-operations.
Checking the list of tickets will be O(n)
(with n
== amount of tickets) - checking if numbers match your winning numbers is fast: O(1)
and avoids if ..:
.
You could do this like so (fully random example):
import random
def random_nums():
"""Returns a tuple of a frozenset of 5 numbers in the range [1..69]
and one number in the range of [1..26] (aka: USA Powerball Lottery)"""
return ( frozenset(random.sample(range(1,70), 5)), random.choice(range(1,27)) )
# draw the win-numbers
winning_nums = set(random.sample(range(1,70), 5))
powerball = random.choice(range(1,27))
# print them
print("Winner: ", *winning_nums, " Powerball: ", powerball)
# generate random tickets
tickets = [ random_nums() for _ in range(10) ]
# check if ticket got something in common with winner numbers
for (nums, power) in tickets:
# fast set operations
intersect = sorted(nums.intersection(winning_nums))
wrong = sorted(nums.difference(winning_nums))
p = 'correct' if power == powerball else 'wrong'
n = "'nothing'"
# some output
print( ( f"You got {intersect or n} correct and guessed "
f"{wrong or n} wrong. Powerball: {p}.") )
Output:
Winner: 14 49 26 27 60 Powerball: 6
You got [49] correct and guessed [21, 41, 59, 66] wrong. Powerball: wrong.
You got [60] correct and guessed [17, 19, 63, 68] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [10, 21, 51, 67, 69] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [18, 30, 40, 45, 52] wrong. Powerball: wrong.
You got [26, 27] correct and guessed [11, 37, 58] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [28, 33, 38, 59, 65] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [11, 18, 35, 61, 64] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [2, 3, 47, 54, 63] wrong. Powerball: wrong.
You got [14] correct and guessed [23, 25, 58, 66] wrong. Powerball: wrong.
You got [27] correct and guessed [47, 52, 56, 58] wrong. Powerball: correct.
See: