Reputation: 501
Okay so I finished a code on hackerrank. It passes the test case but once I submit it fails the others. I have scanned through the code several times without a solution. Any thoughts on this? I have attached the screenshot of the question and my code as well as the test case scenarios that failed.
My code:
#!/bin/python3
import math
import os
import random
import re
import sys
def batting_stats(lst):
rgh,rgf,totr,totbf,totd,crat = 0,0,0,0,0,0 #rgh: runs greater than 100, rgf: runs greater 50
#totr:total runs, totbf:total balls faced, #totd: total
#dismissals, crat: conversion rate
results = []
for inning in lst:
runs_scored = inning[0]
balls_faced = inning[1]
dismissed = inning[-1]
totr += runs_scored
totbf += balls_faced
totd += dismissed
if runs_scored >= 100:
rgh += 1
elif runs_scored == 50:
rgf += 1
average = totr // max(totd, 1)
strikeRate = int((totr / totbf) * 100)
if rgf > 0:
crat = ( rgh // rgf ) * 100
results.append([average, strikeRate, crat])
return results
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
innings_count = int(input().strip())
innings = []
for _ in range(innings_count):
innings_item = list(map(int, input().rstrip().split()))
innings.append(innings_item)
result = batting_stats(innings)
for x in result or []:
fptr.write(' '.join(map(str, x)))
fptr.write('\n')
fptr.close()
Upvotes: 0
Views: 59
Reputation: 438
I don't have access to your grader, but here are some obvious issues I see.
if runs_scored >= 100:
rgh += 1
elif runs_scored == 50:
rgf +=1
should be
if runs_scored >= 100:
rgh += 1
rgf +=1 (because a 100 plus score is also a 50 plus score)
elif runs_scored >= 50: (at least 50 is the condition not exactly 50)
rgf += 1
Next,
average = totr // max(totd, 1)
should be
if totd==0:
average=totr
else:
average=totr/totd
And,
crat = ( rgh // rgf ) * 100
should be
crat = ( rgh / rgf ) * 100
I have included these edits together with a few more, and have tested this code on the one available input and a few others. It returns, as expected, a list of lists with numbers that match the expected output. Please try this out in the grader.
import math
def batting_stats(lst):
rgh,rgf,totr,totbf,totd,crat = 0,0,0,0,0,0
results = []
for innings in lst:
totr += innings[0]
totbf += innings[1]
totd += innings[2]
if innings[0] >= 100:
rgh += 1
rgf +=1
elif innings[0] >= 50:
rgf+=1
if totd==0:
average=totr
else:
average=totr/totd
strikeRate = (totr / totbf) * 100
if rgf > 0:
crat = ( rgh / rgf ) * 100
else:
crat=0
results.append([math.floor(average), math.floor(strikeRate), math.floor(crat)])
return results
Upvotes: 1