I am currently working on a project that collects data from the masslottery.com api. I am analyzing this data in all sorts of ways. One way I would like to analyze this data is by checking what 5 spot keno number has come in the most over, lets say the last year. My program that I have written can theoretically do this. However, it is a bit too slow. Right now checking a 3 spot number takes about a minute and 20 seconds. And as you know the number of possibilities I have to count go up and up as you increase numbers. Since keno is 1-80,
80 choose 3 = 82,160 possibilities
80 choose 4 = 1,581,580 possibilities
80 choose 5 = 24,040,016 possibilities
So as you can see if my program takes a minute on only 80 thousand, it would take like 5 hours to finish for a 5 spot number. With that said I am looking for help on how to speed up my program. I believe my issue is my double for loop but I've tried everything to fix this. I will include my code below.
import requests
from itertools import combinations
import operator
import collections
r = requests.get('https://www.masslottery.com/rest/keno/getDrawsByDateRange?startDate=2021-01-01=&endDate=2021-07-11').json()
draw = (r['draws'])
dict = {}
spot = int(input('How many spots are you trying to check? '))
arr = [
1,2,3,4,5,6,7,8,9,10, 11, 12,
13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80]
test = combinations(arr,spot)
count= 0
print('Starting Check')
for i in test:
dict[str(sorted(i))] = 0
for num in draw:
numbers = num['winningNumbers']
combin = combinations(numbers,spot)
for j in combin:
temp = str(sorted(j))
dict[temp] = dict[temp] + 1
sorted_dict = sorted(dict.items(), key=operator.itemgetter(1))
dict2 = collections.OrderedDict(sorted_dict)
filename = str(spot) + 'spotTest.txt'
with open(filename,'w') as File:
for i in dict2:
File.write(str(i) + '---->'+ str(dict2[i]) +'\n')
print('Done')
NOTE: Also I am aware that keno is a game of luck and has no strategy involved. I am only doing this for my entertainment purposes and to practice coding. Thanks to anyone that has ideas on how to speed this up!
