Problem: Calculate net run rate for a cricket match, given runs scored by team A (batted first), overs played by team A, runs scored by team B, overs played by team B, winning team name, whether team A was bowled out, whether team B was bowled out, total number of overs in one innings.
Solution in Python:
from __future__ import division
import math
def net_run_rate(runsA, oversA, runsB, oversB,
winning_team,
bowled_outA=False,
bowled_outB=False,
total_overs=50,
eps=1e-8):
"""
Author: Mayur P Srivastava
Reference: http://en.wikipedia.org/wiki/Net_run_rate
"""
assert winning_team in ['AB', 'A', 'B']
if winning_team == 'AB':
return 0.0, 0.0
oversA = parse_overs(oversA)
oversB = parse_overs(oversB)
if abs(oversA - total_overs) > eps:
bowled_outA = True
if abs(oversB - total_overs) > eps and winning_team == 'A':
bowled_outB = True
if bowled_outA:
oversA = total_overs
if bowled_outB:
oversB = total_overs
rrA = runsA / oversA
rrB = runsB / oversB
if winning_team == 'A':
nrrA = rrA - rrB
nrrB = -nrrA
else:
nrrB = rrB - rrA
nrrA = -nrrB
return (nrrA, nrrB), (runsA, runsB), (oversA, oversB)
def parse_overs(o):
completed_overs = math.floor(o)
balls = math.floor(0.5 + 10 * (o - completed_overs))
assert balls >= 0 and balls < 6
return completed_overs + balls / 6.0
Concepts Learned: Maths
No comments:
Post a Comment