Can someone please explain why hackerrank does not accept this code for python?
def plusMinus(arr): positive = "{0:.6f}".format(sum(1 for i in arr if i > 0) / len(arr)) negative = "{0:.6f}".format(sum(1 for i in arr if i < 0) / len(arr)) zero = "{0:.6f}".format(sum(1 for i in arr if i == 0) / len(arr)) return "n".join([positive, negative, zero])
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:
if __name__ == '__main__': n = int(input()) arr = list(map(int, input().rstrip().split())) plusMinus(arr)
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:
print("n".join([positive, negative, zero]))