In: Computer Science
Part 1 Lottery Statistics
Create a program that will compute some statistics on winning lottery numbers.
The 10 most frequent numbers are: 55 was drawn 89 times. 66 was drawn 83 times. ... 77 was drawn 71 times. The 10 least frequent numbers are: 11 was drawn 14 times. 22 was drawn 18 times. ... 33 was drawn 26 times.
For the test file i have downloaded the powerball winning numbers in csv format from : https://catalog.data.gov/dataset/lottery-powerball-winning-numbers-beginning-2010
code is in python 3.7, all details have been mentioned in comments
import csv
import operator
with open('powerball_2010.csv') as csv_file:
# converting the csv to dictionary rows
csv_reader = csv.DictReader(csv_file)
# to store the frequency / hash
winning_dict = {}
for row in csv_reader:
# in the csv file the winning row consisits of a sequence of space separated numbers, so we first split by space and cast each string as integer
# then for each integer we check if the value is already present in the dictionary or not and increment its frequency count
winning_numbers = [int(i) for i in row['Winning Numbers'].split(' ')]
for number in winning_numbers:
if number in winning_dict:
winning_dict[number] += 1
else:
winning_dict[number] = 1
# sorting by frequency, each element is a tuple (key,frequency)
sorted_by_frequency = sorted(winning_dict.items(), key=operator.itemgetter(1))
# print(sorted_by_frequency)
# get last 10 elements from array
most_frequent_10 = sorted_by_frequency[-10:]
# get first 10 elements from array
least_frequent_10 = sorted_by_frequency[:10]
print("The 10 most frequent numbers are: ")
for element in reversed(most_frequent_10):
print("{} was drawn {} times".format(element[0], element[1]))
print("\n\nThe 10 least frequent numbers are: ")
for element in least_frequent_10:
print("{} was drawn {} times".format(element[0], element[1]))
Output: