Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Data Science by (18.4k points)

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 the error: 

~ no response on stdout ~

1 Answer

0 votes
by (36.8k points)

You will notice that on the HackerRank your function is called without doing anything with a 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 the new line.

So you should print a 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]))

Do check out Data Science with Python course which helps you understand from scratch.

Browse Categories

...