In: Computer Science
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the range of 1–69, and a “PowerBall” number in the range of 1–26. (You can pick the numbers yourself, or you can let the ticket machine randomly pick them for you.) Then, on a specified date, a winning set of numbers is randomly selected by a machine. If your first five numbers match the first five winning numbers in any order, and your PowerBall number matches the winning Pow-erBall number, then you win the jackpot, which is a very large amount of money. If your numbers match only some of the winning numbers, you win a lesser amount, depending on how many of the winning numbers you have matched.
In the student sample programs for this book, you will find a file named pbnumbers.txt, containing the winning PowerBall numbers that were selected between February 3, 2010 and May 11, 2016 (the file contains 654 sets of winning numbers). Figure 8-6 shows an example of the first few lines of the file’s contents. Each line in the file contains the set of six numbers that were selected on a given date. The numbers are separated by a space, and the last number in each line is the PowerBall number for that day. For example, the first line in the file shows the numbers for February 3, 2010, which were 17, 22, 36, 37, 52, and the PowerBall number 24.
Write a program that works with this file to display the 10 most common numbers, ordered by frequency.
Your submission will consist of:
Thanks for the question, here is the code in Python. Comments are included so that you can follow the code precisely.
Sinceyou didnt shred the pbnumber.txt file, I had to create one dummy file with some dummy records, I validated and teh program seems working fine.
In case you encounter any error when you run with the actual file do comment the error so taht i can help you with the changes.
thank you !
==============================================================================
# file that needs to be read
filename = 'pbnumbers.txt'
# empty dictionary will contain number and its frequency,
number is the lottery number
# which will act as the key while the vlaue will be the number of
times the lottery
# number appeared in the file
number_dict = {}
# open the file in read mode
with open(filename, 'r')
as infile:
# read one line at a time
for line
in infile.readlines():
# split the line by
space
line =
line.strip().split()
# pick the last 6
numbers which will be the lottery numbers
numbers =
line[-6:]
# iterate over each
number
for number in numbers:
# check if the number exist is in the dicitonary
if number_dict.get(number) is
None:
# if its a new number add to the dictionary with count as
1
number_dict[number] = 1
else:
# if the number already exist, increment the value by 1
number_dict[number] += 1
# sort the dictionary in reverse ordder that
is number with highest frquency will apppear first
number_list=sorted(number_dict.items(),key=lambda
pair:pair[1],reverse=True)
# slice the top 10 records
top_10=number_list[:10]
# print the numbers and its frequency
print('{0:<10}{1:>10}'.format('Number','Frequency'))
for data in
top_10:
print('{0:<10}{1:>10}'.format(data[0],data[1]))