Python Challenge: This is a designated challenge problem designed to challenge y
ID: 3753217 • Letter: P
Question
Python Challenge: This is a designated challenge problem designed to challenge your thinking and logic skills. As you encounter challenge problems in the class, please keep things in perspective: these problems are worth only a small number of points. (2 points) The Minnesota Twins are our very own Major League Baseball team. The Twins have not played in the World Series since 1991, so when I have my friends over to watch the World Series we play a simple betting game in order to keep us engaged. It goes like this:
1. Before the start of the game, each friend can buy a number (range 0 - 9) for $5. Friends can buy multiple numbers, but each number can only be "sold" to 1 friend.
2. Each round of play is called an inning, and during an inning each baseball team gets the opportunity to score points (called runs in baseball). There are 9 innings total in a baseball game. At the end of each inning, the scores of the two teams are added together. Whichever friend's number (from #1) matches the last digit of the aggregate score receives a payout of $5.
Of course, there are 10 available numbers (0 - 9) and only 9 innings (payouts), so one number is inevitably left "unsold". For example, suppose my friend Shannon bought the number 6 for $5 before the game. If at the end of the 7th inning the score is 10 to 6 (total of 16), Shannon would receive a $5 payout for the 7th inning. Theoretically a friend who "won" all 9 innings would earn $45 on a $5 initial investment. A 900% payout is nothing to sneeze at! This is serious investing. Of course being a data scientist you don't want to leave anything to chance, so you decide to put your programming skills to work. Write a function named probable_payouts that accepts 2 parameters:
1. The probability ph that the home team scores 1 run during an inning.
2. The probability pa that the away team scores 1 run during an inning.
For simplicity we assume a team either scores 0 or 1 run in each inning. Your function should return a dictionary consisting of the probability-adjusted payouts for choosing each digit 0 - 9.
Let's walk through a visual example (Figure 1). In this example, the probability of each team
scoring a run during an inning is .5. In other words, both arguments to our function are .5. After
the first inning, there is a 25% probability that the score is still 0-0, and also a 25% probability that
the score is 1-1 (total of 2). Note that the values in each column add up to 1. This is a good way
to verify that your intermediate calculations are correct.
At the end of this game, the friend who bought number 2 should have the highest payout. Let's
check this against the output of our function:
THE QUESTION IS... HOW DO YOU CODE THE ALGORITHM THAT PERFORMS THE PAYOUT OPERATION?
inning probable payout total score ends in 1 2 3 4 5 6 78 9 total (total.5) 0 0.25 0.06 0.02 0.00 0.00 0.02 0.06 0.12 0.17 0.70 1 0.50 0.25 0.09 0.03 0.01 0.01 0.02 0.07 0.12 1.10 2 0.25 0.38 0.23 0.11 0.04 0.02 0.01 0.03 0.07 1.14 3 0.00 0.25 0.31 0.22 0.12 0.05 0.02 0.02 0.04 1.03 4 0.00 0.06 0.23 0.27 0.21 0.12 0.06 0.03 0.02 1.01 5 0.00 0.00 0.09 0.22 0.25 0.19 0.12 0.07 0.04 0.98 6 0.00 0.00 0.02 0.11 0.21 0.23 0.18 0.12 0.07 0.93 7 0.00 0.00 0.00 0.03 0.12 0.19 0.21 0.17 0.12 0.85 8 0.00 0.00 0.00 0.00 0.04 0.12 0.18 0.20 0.17 0.72 9 0.00 0.00 0.00 0.00 0.01 0.05 0.12 0.17 0.19 0.55 3.50 5.51 5.71 5.14 5.05 4.88 4.66 4.24 3.58 2.73 ".5 probability of each team scoring 1 run per inningExplanation / Answer
def probable_payouts(ph, pa):
# make a list that can store the score of each inning.
# The maximum that both teams can score in 9 innings is 18.
# That is why we take 19 columns (0 - 18).
# There are 9 innings from 1 to 9.
# But for making calculations easier, we have taken a 0th row,
# 0th row will store 1 in 0th column and 0 in all other columns
winProb = [[0 for i in range(19)] for j in range(10)]
winProb[0][0] = 1
# For each innning (1 to 9)
for inning in range(1, 10):
# Since score can range from 0 to inning*2
for score in range((inning) * 2 + 1):
# Probability of scoring a score is:
# Either both teams score 0 in this inning and sum was score in previous innigs
# OR one of the teams scored 1 in this inning and sum was score-1 in previous innings
# OR both teams scored 1 in this inning and sum was score-2 in previous innings
# NOTE:
# If we want to find the probability for scoring 0,
# we can't check for the -1 or -2 score in previous inning
# Similarly, if we want to find thhe probability for scoring 1,
# we can't check for the -1 score in previous inning
winProb[inning][score] = winProb[inning-1][score]*(1-ph)*(1-pa)
if(score>0):
winProb[inning][score] += winProb[inning-1][score-1]*((1-ph)*(pa)+(ph)*(1-pa))
if(score>1):
winProb[inning][score] += winProb[inning-1][score-2]*(ph)*(pa)
# make a dictionary
payout = {}
for score in range(10):
payout[score] = 0;
# put the sum of all probabilities in the dictionary after multiplying by 5
# Also take %(mod) here to access the last didgit only
for score in range(19):
for inning in range(1,10):
payout[score%10] += winProb[inning][score] * 5
return payout
# Probability of winning
ph = float(input("Enter the probability of winning of home team: "))
while (ph > 1 or ph < 0):
ph = float(input("You can enter a number between 0 and 1 only: "))
pa = float(input("Enter the probability of winning of away team: "))
while (pa > 1 or pa < 0):
pa = float(input("You can enter a number between 0 and 1 only: "))
print(probable_payouts(ph, pa))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.