Question

In: Computer Science

Part 1 Lottery Statistics Create a program that will compute some statistics on winning lottery numbers....

Part 1 Lottery Statistics

Create a program that will compute some statistics on winning lottery numbers.

  • Download a .csv file from Winning Powerball Numbers containing a record of past winning numbers.
  • Examine the formatting of this file.
  • Create a dictionary that contains each winning number and the number of times that number was drawn.
  • Print the 10 most frequently drawn numbers, and the 10 least frequently drawn numbers. HINT: You can’t sort a dictionary. Build a list that is ordered by frequency.
  • The output may look something like the following. (The output below is not correct for your file.)
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.

Solutions

Expert Solution

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:


Related Solutions

Show how to compute the probability of winning the jackpot in the megamillions lottery. The rules...
Show how to compute the probability of winning the jackpot in the megamillions lottery. The rules are at http://www.megamillions.com/how-to-play under ”How to play”. (a) First let us define an appropriate sample space Ω where Ω = {(i1, i2, i3, i4, i5;i6)|what has to hold about i1, . . . , i6}? (b) How many outcomes are in Ω? (c) What is the probability of winning the jackpot? (d) Do you have a better chance of winning the jackpot in powerball...
Part 1: Combinations and Permutations: Winning the Lottery To win the Powerball jackpot you need to...
Part 1: Combinations and Permutations: Winning the Lottery To win the Powerball jackpot you need to choose the correct five numbers from the integers 1 - 69 as well as pick the correct Powerball which is one number picked from the integers 1 - 26. The order in which you pick the numbers is not relevant. You just need to pick the correct five numbers in any order and the correct Powerball. Because there is only one correct set of...
A lottery is conducted in which 7 winning numbers are randomly selected from a total of...
A lottery is conducted in which 7 winning numbers are randomly selected from a total of 62 numbers (1-62). In addition, the Powerball, a single winning number, is selected from an independent pool of 26 numbers (1-26). You select 7 numbers from the pool of 62 numbers. What is the probability that you will select at least one of the winning numbers?
A lottery is conducted in which 7 winning numbers are randomly selected from a total of...
A lottery is conducted in which 7 winning numbers are randomly selected from a total of 62 numbers (1-62). In addition, the Powerball, a single winning number, is selected from an independent pool of 26 numbers (1-26). You select 7 numbers from the pool of 62 numbers. If the probability of choosing 2 of the 7 winning numbers from the pool of 62 numbers is .08862, what is the probability will choose the Powerball from the pool of 26 numbers?
2. Winning the jackpot in a particular lottery requires that you select the correct three numbers...
2. Winning the jackpot in a particular lottery requires that you select the correct three numbers between 1 and 2828 and, in a separate​ drawing, you must also select the correct single number between 1 and 3232. Find the probability of winning the jackpot. 3. Winning the jackpot in a particular lottery requires that you select the correct five numbers between 1 and 4040 ​and, in a separate​ drawing, you must also select the correct single number between 1 and...
Winning the jackpot in a particular lottery requires that you select the correct five numbers between...
Winning the jackpot in a particular lottery requires that you select the correct five numbers between 1 and 25 and, in a separate drawing, you must also select the correct single number between 1 and 37. Find the probability of winning the jackpot. The probability of winning the jackpot is _____
1. In an instant lottery, your chances of winning are 0.1. If you play the lottery...
1. In an instant lottery, your chances of winning are 0.1. If you play the lottery six times and outcomes are independent, determine the probability that (i) you win at most once. (ii) you lose all six times. (iii) you win exactly two times. Please show work will rate!!!
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player chooses ten different numbers and hopes that these get drawn. If the player's numbers get drawn, he/she wins an obscene amount of money. The table below displays the frequency with which classes of numbers are chosen (not drawn). These numbers came from a sample of 180 chosen numbers. Chosen Numbers (n = 180) 1 to 10 11 to 20 21 to 30 31 to...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player chooses ten different numbers and hopes that these get drawn. If the player's numbers get drawn, he/she wins an obscene amount of money. The table below displays the frequency with which classes of numbers are chosen (not drawn). These numbers came from a sample of 180 chosen numbers. Chosen Numbers (n = 180) 1 to 10 11 to 20 21 to 30 31 to...
The odds of winning a certain lottery with a single ticket is 1 in 300,000,000. In...
The odds of winning a certain lottery with a single ticket is 1 in 300,000,000. In May and June, 200,000,000 tickets were bought. 1. Please assume the tickets win or lose independently of each other and give the exact probability that there was no winner during the two months. 2. Only using a basic scientific calculator, give an approximation to the same question from part 1. Explain why this approximation is a good one.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT