测试用例在HackerRank中失败了。

huangapple go评论63阅读模式
英文:

Test case failed in hackerRank

问题

def plusMinus(arr):
# 在此编写您的代码
a = 0
b = 0
c = 0
pos, neg, zer = 0, 0, 0
for i in arr:
if (i >= 1):
a += 1
pos = a / len(arr)
if (i <= -1):
b += 1
neg = b / len(arr)
if (i == 0):
c += 1
zer = c / len(arr)

return(f'{pos:.6f}\n{neg:.6f}\n{zer:.6f}')

arr = [-4, 3, -9, 0, 4, 1]
plusMinus(arr)
print(plusMinus(arr))

英文:

Can any one help me out?
Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.
Here is link----> (https://www.hackerrank.com/challenges/plus-minus/problem?isFullScreen=true&amp;h_r=next-challenge&amp;h_v=zen&amp;h_r=next-challenge&amp;h_v=zen)

def plusMinus(arr):
    # Write your code her
    a  = 0
    b = 0
    c = 0
    pos, neg, zer = 0,0,0
    for i in arr:
        if (i &gt;= 1):
            a +=1
            pos = a / len(arr)  
        if (i &lt;= -1):
            b+=1
            neg = b/len(arr)
        if (i==0):
            c+=1
            zer = c/len(arr) 
    
    return(f&#39;{pos:.6f}\n{neg:.6f}\n{zer:.6f}&#39;)
arr = [-4,3,-9,0,4,1]
plusMinus(arr)
print(plusMinus(arr))

</details>


# 答案1
**得分**: 1

请注意网站上挑战开始时不要删除的代码部分:

```python
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'plusMinus' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def plusMinus(arr):
    # 写入你的代码在这里

if __name__ == '__main__':
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)

请注意 # 写入你的代码在这里,不要修改代码的其他部分,因为它们是使测试用例正常运行所必需的。

将你的函数代码粘贴到正确的位置,并确保它实际打印自己,将会得到满分:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'plusMinus' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def plusMinus(arr):
    # 写入你的代码在这里
    a  = 0
    b = 0
    c = 0
    pos, neg, zer = 0,0,0
    for i in arr:
        if (i >= 1):
            a +=1
            pos = a / len(arr)  
        if (i <= -1):
            b+=1
            neg = b/len(arr)
        if (i==0):
            c+=1
            zer = c/len(arr) 
    
    print(f'{pos:.6f}\n{neg:.6f}\n{zer:.6f}')

if __name__ == '__main__':
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)

这样可以获得所需的输出。

英文:

Don't delete the code that is on the website when the challenge starts

In your case, the starting code looks like this:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the &#39;plusMinus&#39; function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def plusMinus(arr):
    # Write your code here

if __name__ == &#39;__main__&#39;:
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)

note the # Write your code here, don't alter the other parts of the code as they are needed to make the testcases work.

Simpyly pasting your code of the function to the correct place and making it actually print itself will give full points:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the &#39;plusMinus&#39; function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def plusMinus(arr):
    # Write your code her
    a  = 0
    b = 0
    c = 0
    pos, neg, zer = 0,0,0
    for i in arr:
        if (i &gt;= 1):
            a +=1
            pos = a / len(arr)  
        if (i &lt;= -1):
            b+=1
            neg = b/len(arr)
        if (i==0):
            c+=1
            zer = c/len(arr) 
    
    print(f&#39;{pos:.6f}\n{neg:.6f}\n{zer:.6f}&#39;)

if __name__ == &#39;__main__&#39;:
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)

brings the desired output

答案2

得分: 0

print(f'{pos:.6f}\n{neg:.6f}\n{zer:.6f}')

英文:

The function should not return the value. It should print the value in each line. Change your return to print

print(f&#39;{pos:.6f}\n{neg:.6f}\n{zer:.6f}&#39;)

答案3

得分: 0

你的代码是正确的,只是没有满足问题本身的要求,问题陈述为:“打印每个...” 你使用了return,根据定义,它将正确的输出传递到全局代码,但从未被打印出来,因此测试用例失败。

英文:

Your code is correct, just did not satisfy the question itself, which states: Print the decimal value of each ...
You used return, which by definition passes that correct output out into the global code, but it never gets printed, thus the test cases are failing.

huangapple
  • 本文由 发表于 2023年7月24日 18:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定