

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures are Data Structures, Dynamic Programming, First-In-First-Out, Implementation, Python Code. Main points of this lecture are: Python Code, Partial Program, Sided Dice, Highest Percentage, Functional-Decomposition, Outline, Message, User, Number of Times, Tallies
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Objective: To practice writing Python code.
To start the lab: Download and unzip the file lab1.zip from
Part A: In the folder lab1, open the diceOutcomes.py program in IDLE. (Right-click on diceOutcomes.py | Edit with IDLE) It contains a partial program we started to discuss in class to solve the problem:
“Write a program to roll two 6-sided dice 1,000 times to determine the percentage of each outcome (i.e., sum of both dice). Report the outcome(s) with the highest percentage.”
I decided to functional-decomposition this problem as:
main
displayWelcome calculateFrequentRolls
displayResults
rollAndTallyOutcomes findOutcomes
mostFrequentRolls highestCount
mostFrequentRolls highestCount
outcomeCounts outcomeCounts highestCount
outcomeCounts
max
mostFrequentRolls highestCount
main - provides an outline of program by calling top-level functions displayWelcome - Displays welcome message for the user calculateFrequentRolls - Rolls the dice the correct number of times, tallies the outcomes, and returns a list of outcomes with the highest count and highest count. rollAndTallyOutcomes - Rolls the dice the correct number of times and tallies the outcomes. Returns a list of tallies with the index being the outcome. max - built-in function to return the largest item in an iterable data structure like a list. findOutcomes - Returns a list of outcomes with the highest count. displayResults - Displays the outcome(s) with the highest percentage.
Consider running the program with only 10 dice rolls instead of 1,000. The program output with some extra debugging prints showing the two Python lists used: outcomeCounts and mostFrequentRolls.
This programs rolls two 6-sided dice 10 times to determine the outcome(s) with the highest percentage. outcomeCounts: [0, 0, 1, 0, 2, 1, 0, 3, 0, 0, 3, 0, 0] mostFrequentRolls: [7, 10] and highestCount: 3 The highest percentage is 30.0 for outcome(s): 7 10
Your task for lab 1 is to complete the code for the rollAndTallyOutcomes and findOutcomes functions.
After you have working code, raise your hand and demonstrate your code for me or the TA (Morgen).
If you complete all parts of the lab, nothing needs to be turned in for this lab. If you do not get done today, then show me the completed lab in next week’s lab period. When done, remember save your program to a USB drive (or email to yourself).
Lab 1 - 1
EXTRA CREDIT -- Part B: Rewrite the program using a dictionary instead of a list for the outcomeCounts. Your dictionary will have the outcome for the key and its corresponding tally as its value.
After you have working code, raise your hand and demonstrate your code for me or the TA (Morgen).
Lab 1 - 2