2

The challenge is

Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.

This is my code for it:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the plusMinus function below.
def plusMinus(arr):
    l = len(arr) #get length of the array
    p = 0 #positive ints
    n = 0 # negative ints
    z = 0 # zeroes
    for i in range(0, len(arr)):
        if arr[i] > 0:   #check if num is positive
            p = p+1
        elif arr[i] < 0:    #check if num is negative
            n = n+1
        else: z = z+1     #check if num is zero

    return p/l, n/l, z/l

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

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

    plusMinus(arr)

For some reason it says that my output is

~ no response on stdout ~

So I tried putting the function inside a print function like that:

print(plusMinus(arr))

And it does output the correct answers to the challenge, but I'm guessing it's not the right format hackerrank expects, it says the output is:

(0.5, 0.3333333333333333, 0.16666666666666666)

instead of:

0.500000 0.333333 0.166667

What am I doing wrong? thanks in advance.

Skity
  • 131
  • 1
  • 10

3 Answers3

1

In python when you return multiple values from a function, it returns a tuple with the values in it. In your case I would print each variable of the tuple.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the plusMinus function below.
def plusMinus(arr):
    l = len(arr) #get length of the array
    p = 0 #positive ints
    n = 0 # negative ints
    z = 0 # zeroes
    for i in range(0, len(arr)):
        if arr[i] > 0:   #check if num is positive
            p = p+1
        elif arr[i] < 0:    #check if num is negative
            n = n+1
        else: z = z+1     #check if num is zero

    return p/l, n/l, z/l

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

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

    p, n, z = plusMinus(arr)
    print(p, n, z)
Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
0

You did not provide the input, and your code doesn't print anything, so even if I could run it, it wouldn't reproduce the issue you describe. Fortunately, it's not too hard to guess that you have an issue with "how to return multiple values from a python function"

Your function has multiple return values, therefore it returns a tuple. When you print a tuple, it has parentheses.

def f():
    return 1/2, 1/3, 1/6

t = f()
print (t)
a, b, c = f()
print (a, b, c)

(0.5, 0.3333333333333333, 0.16666666666666666)
0.5 0.3333333333333333 0.16666666666666666

You could just unpack the tuple with *, so you pass each element as a separate argument to print, like so:

print (*f())

But normally if you are returning multiple values, it's a good idea to have well named variables for each of the returns, so it is clear in the calling code what is going on.

positives, negatives, zeroes = get_plusminus_ratios(data)

This link appears to have a good bit of info How do I return multiple values from a function?

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
0

You have not printed p,n and z after computing them anywhere, you just return them and when they are returned back to the code where the function was called, they are not stored in any variables and aren't printed, that's why it says "no output on stdout" try :

def plusMinus(arr):
    l = len(arr) #get length of the array
    p = 0 #positive ints
    n = 0 # negative ints
    z = 0 # zeroes
    for i in range(0, len(arr)):
        if arr[i] > 0:   #check if num is positive
            p = p+1
        elif arr[i] < 0:    #check if num is negative
            n = n+1
        else: z = z+1     #check if num is zero

    print (p/1, n/1, z/1) # Changed Part

hope this works

also I think p,n,z need not be divided by "/1" as they are already integers.