Can someone please explain why hackerrank does not accept this code for python?
JavaScript
x
6
1
def plusMinus(arr):
2
positive = "{0:.6f}".format(sum(1 for i in arr if i > 0) / len(arr))
3
negative = "{0:.6f}".format(sum(1 for i in arr if i < 0) / len(arr))
4
zero = "{0:.6f}".format(sum(1 for i in arr if i == 0) / len(arr))
5
return "n".join([positive, negative, zero])
6
It gives me this error: ~ no response on stdout ~
Advertisement
Answer
You will notice that on HackerRank your function is called without doing anything with the return value. The template code looks like this:
JavaScript
1
7
1
if __name__ == '__main__':
2
n = int(input())
3
4
arr = list(map(int, input().rstrip().split()))
5
6
plusMinus(arr)
7
Moreover, the description says:
Print the decimal value of each fraction on a new line.
So you should print the result. And since your code doesn’t print anything, the error message is what could be expected.
Instead of return
, do:
JavaScript
1
2
1
print("n".join([positive, negative, zero]))
2