Skip to content

Instantly share code, notes, and snippets.

@nsa-yoda
Created September 13, 2017 20:34
Show Gist options
  • Save nsa-yoda/dae59adb3ab1c126fef8a8676376102c to your computer and use it in GitHub Desktop.
Save nsa-yoda/dae59adb3ab1c126fef8a8676376102c to your computer and use it in GitHub Desktop.
Fetches JSON results from the NY lottery for lotto or mega and calculates mean and top 5 most recurring numbers (along with count)
import sys, urllib, json, math, operator, datetime
today = datetime.datetime.now()
endSearchDate = str(today.month + 1 if today.month + 1 <= 12 else today.month) + '/' + str(today.year)
game = 'mega' if sys.argv[1] == 'mega' else 'lotto'
url = 'http://nylottery.ny.gov/wps/PA_NYSLNumberCruncher/NumbersServlet?game='+game+'&action=winningnumbers&startSearchDate=08/2000&endSearchDate='+endSearchDate+'&pageNo=1&last=0&perPage=10000&sort=0'
datas = json.loads((urllib.urlopen(url)).read())
matrix = []
for data in datas['draw']:
nums = []
for num in data['numbersDrawn'].split('-'):
nums.append(int(num))
nums.append(data['bonusNumberDraws'] if game == 'lotto' else data['megaBallDrawn'])
matrix.append(nums)
print "mean", [ int(math.ceil(float(sum(l))/len(l))) for l in zip(*matrix)]
counter = {1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}}
for mset in matrix:
for i in range(1, (8 if game == 'lotto' else 7)):
if mset[i-1] in counter[i]:
counter[i][mset[i-1]] = counter[i][mset[i-1]] + 1
else:
counter[i][mset[i-1]] = 1
for i in range(1, (8 if game == 'lotto' else 7)):
print i, sorted(counter[i].items(), key=operator.itemgetter(1))[-6:-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment