通过第一种情况,但未通过其他情况

时间:2019-10-20 03:54:42

标签: python-3.x

好的,所以我完成了关于hackerrank的代码。它通过了测试用例,但是一旦我提交它就使其他人失败。我已经多次扫描了该代码,但没有解决方案。有什么想法吗?我已经附上了问题和我的代码以及失败的测试用例场景的屏幕截图。

enter image description here

Cases ran against

我的代码:

#!/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()

1 个答案:

答案 0 :(得分:1)

我无权访问您的平地机,但是我看到了一些明显的问题。

if runs_scored >= 100:
        rgh += 1
    elif runs_scored == 50:
        rgf +=1

应该是

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

下一步

 average = totr // max(totd, 1)

应该是

          if totd==0:
           average=totr
          else:
           average=totr/totd

然后 crat = ( rgh // rgf ) * 100应该是

crat = ( rgh / rgf ) * 100  

我将这些编辑以及其他一些编辑包括在内,并在一个可用的输入和其他几个输入上测试了此代码。如预期的那样,它返回一个列表列表,列表的编号与预期的输出匹配。请在平地机上尝试一下。

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