Computer science project

profileJerry Shen
Project2_vN14.pdf

CSCI 140 Programming for Data Science Project 2 Checkpoint Deadline 1700, Sunday September 29

Due 1700, Friday, October 4

In our second project, you are tasked with developing functions to simulate playing the lottery. You will create two functions, correct a third function, and write a short main program.

N.B.: You must use the structures we have learned in this class to complete this assignment. Implementations using structures we have not covered may receive no credit. For example, you may not use string and list methods since we have not formally talked about methods.

We have provided two skeleton files: Project_2.py for your function code, and Project_2_Main.py for your main program.

Function One: Generating Lottery Numbers

Many states in the United States have a state lottery. Players pick typically between 4 and 6 numbers within a certain range, and then win monetary prizes based on how many of their numbers match numbers randomly drawn in the lottery. For example, a lottery could use the numbers from 1 to 50, and players must pick 5 numbers. Each number can only be chosen once, that is, each number is unique. For example, a player could choose: 39 7 12 14 42

Write a function called generate_numbers that randomly selects lottery numbers. This function has two optional arguments: limit – the maximum number used in the lottery, i.e. the highest number a player can choose, and num – the number of lottery numbers to select. The default value of limit is 34 and the default value of num is 5. These defaults correspond to one of the Virginia lottery games. Your function will return a list of randomly selected lottery numbers. Here are some examples – numbers are selected randomly so these show only 1 possible outcome.

• generate_numbers with default values for limit and num

Example return value: [20, 10, 34, 12, 31]

• generate_numbers with default value for limit, num = 8:

Example return value: [16, 14, 12, 28, 26, 15, 30, 32]

• generate_numbers with limit = 55, num = 4:

Example return value [11, 26, 18, 48]

You can assume that the input for limit will always be a positive integer greater than or equal to num. The input for num will be a positive integer.

Key points:

• Optional argument limit gives the highest possible lottery number that can be chosen; inputs tested will always be correct type

• Each lottery number can only be chosen ONCE, they cannot be repeated • Optional argument num is an integer; default value: 5; inputs tested always correct type • Return a list of num integers randomly selected with no duplicates between 1 and limit • You can do this with a loop, but it is actually easier to use one of the functions from the

random module without a loop; HINT: how can we generate the numbers from 1 to limit?

Function Two: Win or Lose

To win the lottery, you must match your numbers to the winning numbers. The prize you win corresponds to how many of your numbers match the winning numbers. The more numbers you match, the larger the prize. For example, if your numbers are: 15 32 42 10 18 and the winning numbers are: 32 42 15 18 9, you have matched four out of five numbers and would win the second largest prize. Typically, you don’t win a prize for matching very few numbers, e.g. in the Virginia lottery, you won’t get anything if you match only one or two numbers to the winning numbers.

You have been code for a function called lotto_prize which needs to be corrected. You may not add or delete lines. You may change indentation, but should not change the order of the lines.

The function lotto_prize takes three required arguments: attempt – a list containing a player’s lottery numbers, winning – the winning lottery numbers, and prizes – a list containing the prize amounts from largest to smallest. The function will return a numeric value that is the prize won, either an item from prizes or 0. Whether and how much the player wins, and which output is returned, is based on how many numbers match. Matching all numbers gets the largest prize, all but one the second largest, and so forth. If they match too few numbers, they win nothing.

The examples below are for demonstration only, your function should PRINT NOTHING.

• All of the player’s numbers match: player wins the largest prize

attempt is [28, 49, 2, 55, 33, 44], winning is [2, 28, 49, 33, 44, 55]

prizes is [1000000, 400, 20, 5]

Function returns 1000000

• All but one of the player’s numbers match: player wins the second largest prize

attempt is [28, 49, 2, 33, 44], winning is [2, 28, 9, 33, 44]

prizes is [1000, 40, 1]

Function returns 40

• Player matches too few numbers to win: player wins nothing

attempt is [28, 49, 2, 33, 44, 19, 4], winning is [50, 49, 27, 31, 2, 7, 11]

prizes is [1250000, 10000, 1500, 50]

Function returns 0

The inputs for attempt and winning will always be of the same length, but that length can be any positive integer. The input for prizes is a list sorted from largest to smallest with a length less than or equal to the length of attempt and winning. No other inputs will be tested.

Key points:

• The text of the code you are given is ALMOST correct and requires only minor changes. These changes mostly have to do with logical errors and things like indexing. You need to preserve as much of the original code as possible. You may need to change indentation for some lines. You do not need to and are not permitted to add any lines or delete any lines. You must understand in detail what each line is meant to do and how to solve the problem on paper. READ THE LAST KEY POINT AND COMMENTS IN CODE VERY CAREFULLY.

• All of the required arguments are lists: attempt and winning are the same length and are unsorted; prizes has a length less than or equal to attempt and winning and is sorted largest to smallest - this means the user inputs it sorted, your function does not sort it.

• Function returns an integer that is either the prize amount of 0; prize won depends on how many numbers match

• The function is written to operate in this general manner: check if each item from attempt is part of winning, count how many items match. Use count of matches to determine if the player wins a prize, and if so, which prize from prizes they win. Return prize amount.

Function Three: Can you really win?

You decide to play a set of lottery numbers until you win the jackpot, that is until your numbers match all of the winning numbers. How many times would you have to play before this happens?

Write a function called simulate_lotto that takes two required arguments: your_numbers –a list containing your lottery numbers and max_pick – the highest number available to pick in the lottery – that is, the highest value that could appear in your numbers or the winning numbers. simulate_lotto also takes an optional argument max_plays which has a default value of 10,000.

The purpose of max_plays is to prevent your code from running forever. In a lottery with 6 numbers chosen from 1 to 50, there are 11,441,304,000 different sets of winning numbers, which doesn’t even account for the same numbers being chosen twice. Your program could run for a LONG time before the player’s numbers win. max_plays limits how many times winning numbers will be chosen. Even if the player hasn’t won, the function will stop after max_plays lottery picks.

simulate_lotto should return an integer which is the number of plays with your numbers it took to win the jackpot –this means all of the numbers match- OR max_plays, whichever is smaller. Here are some examples to demonstrate how the function works. This is NOT printed OUTPUT.

• Example #1: your_numbers is [45, 23, 12, 22], max_pick is 50, max_plays is 10,000

Play 1: winning numbers are [12, 49, 2, 45]

Play 2: winning numbers are [7, 8, 41, 13]

Play 3: winning numbers are [12, 23, 45, 22]

Function returns 3. Notice that the order of the numbers doesn’t matter, just that the winning numbers match your_numbers. Once they match, the function terminates.

• Example #2: Example #1: your_numbers is [5, 2, 1], max_pick is 10, max_plays is 4

Play 1: winning numbers are [1, 4, 2]

Play 2: winning numbers are [7, 8, 4]

Play 3: winning numbers are [1, 3, 4]

Play 4: winning numbers are [1,7,9]

Function returns 4 – the value of max_plays. Even though the player didn’t win (winning numbers were never [5,2,1]), the function terminates because the max number of plays was reached.

Key points:

• Input for your_numbers will always be a list of integers. The length of this list can vary. • Input for max_pick will always be a positive integer greater than or equal to the length

of your_numbers. Input for max_plays will always be a positive integer. • The winning numbers are generated by your function – they are NOT input by the user.

You MUST use your generate_numbers function. Figure out how many numbers to generate from the user input your_numbers – HINT: look at the length of the input.

• You MUST use your lotto_prize function to determine whether or not the user won the jackpot. Winning the jackpot means that all numbers match. Think about how to specify the prizes argument to make this work.

• You need to use a loop in this function. We have not discussed break or continue. Do not use them. Stop your loop either using logic or by exploiting how return works.

Main Program: Playing the Lottery

You will write a brief main program that makes use of your functions. This must be saved and submitted in Project_2_Main.py. The main program must make use of your functions. You will be graded specifically on coding style for this part of the project – that means writing efficient code that is not repetitive, and does not specifying default arguments when possible. The main program should do the following:

• Generate five lottery numbers to play where the numbers range from 1 to 65. Use one of your functions to do this.

• Generate a second set of five lottery numbers to be the winning numbers. These also range from 1 to 65. Use one of your functions to do this.

• Using the two sets of numbers you generated above, calculate the prize the user wins. The available prizes are: $100000 for all matching numbers, $500 for 4 matching numbers, and $50 for 3 matching numbers. Use one of your functions to do this. Print out the prize.

• Generate six lottery numbers to play where the numbers range from 1 to 20. Use one of your functions to do this. Simulate playing these numbers until you win the jackpot or you reach 1000 plays. Use one of your functions to do this. Print out how many plays it took.

Extra Credit

Worth up to 3% total. Fill in under def lines in Project_2_EC.py

1) Many lotteries have a final number drawn that is a “super pick” which has its own prizes and/or can double/triple/etc. the prize won using the other numbers. For example, you might pick 5 regular lottery numbers between 1 and 50 and then pick a final number between 1 and 20:

45 2 5 14 29 Super pick: 11

Fill in the function definition for generate_numbers_super to add support for a single extra “super” pick. Keep in mind that the range of numbers for the super pick may be different from the range for the regular lottery numbers, and this function has an additional parameter super_limit which is the upper bound for the “super pick” (the highest number the super pick can be). The return value is a list where the LAST item in the list is the super pick.

2) Fill in the function definition for lotto_prize_super to add support for a super pick as described in 1. This function has two additional arguments, s_num - a Boolean indicating if the lottery numbers provided indicate a super pick as the last number, and multiplier – this indicates what to multiply the prize by if the user wins. Here’s an example of calling the function:

lotto_prize_super([43,57,23,45,1], [40, 45, 32, 23,1],[100000,100, 10], True, 2)

In this case, since s_num is True, 1 is separated from the other numbers. The user matched two regular numbers (45 and 23), so they would win $10. Since they also matched the super pick and multiplier is 2, this is doubled and the value returned is 20.

3) The corrected implementation of lotto_prize finds matches a particular way. It might be more efficient to determine matches by sorting the lists first and/or implementing a search algorithm. Or you could implement this function more concisely by using objects other than lists. Fill in the function definition for lotto_prize_ search with any enhancements you can come up with.

SUBMISSION EXPECTATIONS

Project_2.py Your implementations of functions one, two (corrected), and three. Do not change the names of parameters in the function def lines or the names of the functions – if you do so, it will affect your grade. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines. If your code requires manual intervention to run such as adding/editing import lines, correcting indentation, removing print and input lines, etc., and/or correcting syntax errors, you can expect up to a 5 point deduction.

Project_2_Main.py Your implementation of the main program specified in Part 4. Your functions must not be repeated in this file. They must be imported using the correct import line. Make sure that you have the correct import lines. If your code requires manual intervention, you can expect up to a 5 point deduction.

Project_2_EC.py Your implementations of any of the extra credit functions. They must be submitted in this file. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines.

Project_2.pdf A PDF document containing your reflections on the project. You must also cite any sources you use. Please be aware that you can consult sources, but all code written must be your own. Programs copied in part or wholesale from the web or other sources will receive 0 points and will result in reporting of an Honor Code violation.

N.B.: You must use the structures we have learned in this class to complete this assignment. Implementations using structures we have not covered may receive no credit. This means NO LIST AND STRING METHODS (yes, that includes append – do not use it).

POINT VALUES AND GRADING RUBRIC

Function 1: 27.5 points

Function 2: 27.5 points

Function 3: 32.5 points

Main Program: 10 points

Write-up: 2.5 points

SUGGESTIONS FOR COMPLETION TIMELINE

This project integrates concepts from many lectures. The most important step will be to understand what each function does and figure out the necessary steps. Make sure to work things out on paper first. This schedule sets you up to submit all functions for the checkpoint.

-Read through instructions and make sure you understand what each function does and how it works by 9/23 – bring questions to office hours and lab; start writing steps on paper ASAP

-Complete debugging Function 2 after lecture 9/23-9/24

-Complete Function 1 and first three steps of main program after lecture 9/25-9/26 (you could complete this after lecture 9/23-9/24 but it will be easier with material from 9/25-9/26)

-Complete Function 3 and last step of main program after lecture 9/25-9/26 (you could complete this after 9/23-9/24 but it will be easier with material from 9/25-9/26)