Answer to Question #193347 in Python for SRIVARSHAN

Question #193347

It is the final of the world’s most prestigious cricket tournament, the Indian Premier League. In the final, the Chennai Super Kings (CSK) are playing against the Mumbai Indians (MI). There is one over left for the match to end and CSK require R runs to win with W wickets remaining.


An over consists of 6 balls. Each ball can result in any one of the following possibilities:-


  • 6 Runs
  • 4 Runs
  • 7 Runs (3 Runs + Overthrows)
  • 2 Runs
  • 1 Run
  • Wicket

If a ball results in a wicket, CSK’s remaining wickets will decrease by one. If a ball results in runs (either 6, 4, 7, 2 or 1), these runs are added to the runs that CSK have already scored.


The match ends when one of the following happens:-


* If CSK scores R runs, the match ends and CSK wins.


* If the match ends with CSK scoring exactly one run less than the required runs, then the match is a tie.




1
Expert's answer
2021-05-14T23:06:31-0400
def outcomeSum(list_,R,W): #returns me a list of the least number of balls played to get an outcome of the match
s = []
runs = 0
wicket = 0
try:
for i in list_:
s.append(i)
try:
runs += int(i)
if runs >= R: #when the run exceeds the required runs break the loop and return the list
s.append("win")
break
except:
wicket += 1
if wicket == W and runs == R -1: #when all wickets are down, and run is 1 less than required runs return list with tie at last
s.append("tie")
break
elif wicket == W and runs < R -1: #when all wickets are down, and run is less than required runs -1 return list with loss at last
s.append("loss")
break
except:
pass
return s

import itertools
outcome = ["6","4","7","2","1","W"]
input_ = input().split(" ")
R = int(input_[0])
W = int(input_[-1]) ##taking input

win = 0 #to store the result
tie = 0
loss = 0
MAX = max([R,W]) #getting the minimum of iteratios to be done
MAX = min([6,MAX])
all_outcomes = [] #saves all the possible already gotten outcomes
for i in range(1,MAX+1):
for p in itertools.product("".join(outcome), repeat=i): #we are doing a cartesian product to get the various outcomes
compressed = outcomeSum(list(p),R,W) #getting result
o = compressed[-1] #storing win/tie/loss in o
compressed = compressed[:-1] #deleting the result at last position
if o == "win" and compressed not in all_outcomes: #if result is win and outcome is not there in already gotten outcomes we increment win by 1
all_outcomes.append(compressed)
win += 1
elif o == "tie" and compressed not in all_outcomes: #if result is tie and outcome is not there in already gotten outcomes we increment tie by 1
all_outcomes.append(compressed)
tie += 1
elif o == "loss" and compressed not in all_outcomes: #if result is loss and outcome is not there in already gotten outcomes we increment loss by 1
all_outcomes.append(compressed)
loss += 1
  
print("\n",win,tie,loss) #printing the output

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS