Question

In: Computer Science

To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...

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.

Solutions

Expert Solution

Thanks for the question.
ere is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 


If you are satisfied with the solution, please rate the answer. 


Thanks

===========================================================================

# 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]))

=========================================================================


Related Solutions

To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...
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...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...
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...
1) You buy a lottery ticket to a lottery that costs $10 per ticket. There are...
1) You buy a lottery ticket to a lottery that costs $10 per ticket. There are only 100 tickets available to be sold in this lottery. In this lottery there are one $500 prize, two $100 prizes, and four $25 prizes. Show its probability distribution in the form of a table. What is the standard deviation of your gain or loss? What type of skewness does the probability distribution represent?
A lottery ticket is given. For this lottery, five integer numbers are chosen from 1 to...
A lottery ticket is given. For this lottery, five integer numbers are chosen from 1 to 50, and the order doesn’t matter. You win the main prize of one million dollars if your ticket matches all five numbers drawn; you win a second prize of one thousand dollars if your ticket matches exactly four of the five numbers drawn; and otherwise you win nothing. a. What’s the probability that you win the main prize? b. What’s the probability that you...
Let’s suppose that you are going to play the lottery game Powerball. To play, you pick...
Let’s suppose that you are going to play the lottery game Powerball. To play, you pick five different numbers from 1 through 69 plus one Powerball number from 1 through 26. Which is a more likely combination of winning numbers: 1, 2, 3, 4, 5, 6 or 7, 21, 25, 32, 40, 56? Explain your answer. For a $500,000,000 jackpot, which of the two combinations would likely be more lucrative for you if it were to win? In other words,...
Recall that the Powerball Lottery involves selecting five numbers from 1 to 69 for the white...
Recall that the Powerball Lottery involves selecting five numbers from 1 to 69 for the white balls and then selecting one number from 1 to 26 for the red Powerball. The Lottery does not give a prize for matching exactly two white balls without a matching red ball. Why do you think that prizes are not given for this combination? Justify your answer using probabilities.
Recall that the Powerball Lottery involves selecting five numbers from 1 to 69 for the white...
Recall that the Powerball Lottery involves selecting five numbers from 1 to 69 for the white balls and then selecting one number from 1 to 26 for the red Powerball. The Lottery does not give a prize for matching exactly two white balls without a matching red ball. Why do you think that prizes are not given for this combination? Justify your answer using probabilities.
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
5) In the MegaMillions lottery a player buys a $1 ticket and picks five numbers from...
5) In the MegaMillions lottery a player buys a $1 ticket and picks five numbers from the numbers 1 to 70. The lottery has a bin with seventy white balls, each with a number from 1 to 70. The lottery picks five balls from the bin with the white balls. If the five numbers the player picked match the numbers drawn on the five white balls then the player wins $1,000,000 (in most states, but not including California, where the...
You are planning to buy a lottery ticket for the $5,000,000 jackpot. If you have (Option...
You are planning to buy a lottery ticket for the $5,000,000 jackpot. If you have (Option A). the cash option of receiving $5,000,000 today or you can opt to choose (Option B). the 20 payments of $400,000. Your investment account earns 6% interest compounded annually. What is the present value of OPTION B? (Please use two decimals).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT