Plus Minus


Given an array of integers, calculate the fractions of its elements that are positivenegative, and are zeros. Print the decimal value of each fraction on a new line.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to  are acceptable.
Input Format
The first line contains an integer, , denoting the size of the array. 
The second line contains  space-separated integers describing an array of numbers .
Output Format
You must print the following  lines:
  1. A decimal representing of the fraction of positive numbers in the array compared to its size.
  2. A decimal representing of the fraction of negative numbers in the array compared to its size.
  3. A decimal representing of the fraction of zeros in the array compared to its size.
Sample Input
6
-4 3 -9 0 4 1         
Sample Output
0.500000
0.333333
0.166667
Explanation
There are  positive numbers,  negative numbers, and  zero in the array. 
The proportions of occurrence are positive: , negative:  and zeros: .

Solution:
Python Code
#!/bin/python3

import os
import sys

#
# Complete the plusMinus function below.
#

def plusMinus(arr):
    #
    # Write your code here.
    #
    one = 0
    two = 0
    three = 0
    for x in arr:
        if(x>0):
            one = one + 1
        elif(x<0):
            two = two + 1
        else:
            three = three + 1
    print(one/n)
    print(two/n)
    print(three/n)



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

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

    plusMinus(arr)

Comments

Popular Posts