Plus Minus - Hacker Rank Solution

Hello coders, today we are going to solve Plus Minus Hacker Rank Solution. Given an array of integers, calculate the ratios of its elements that are p

Hello coders, today we are going to solve Plus Minus Hacker Rank Solution.

👊 Table Of Contents

Task

Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to 10^(-4) are acceptable.

Example

arr = [1, 1, 0, -1, -1]

There are n = 5 elements, two positive, two negative and one zero. Their ratios are 2/52/5 and 1/5. Results are printed as:

0.400000
0.400000
0.200000

Function Description

Complete the plusMinus function in the editor below.

plusMinus has the following parameter(s):

int arr[n]: an array of integers

Print

Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with 6 digits after the decimal. The function should not return a value.

Input Format

The first line contains an integer, n, the size of the array.

The second line contains n space-separated integers that describe arr[n].

Constraints

0 < n <= 100 

-100 <= arr[i] <= 100

Output Format

Print the following  lines, each to  decimals:

  • proportion of positive values
  • proportion of negative values
  • proportion of zeros

Sample Input

STDIN           Function
-----           --------
6               arr[] size n = 6
-4 3 -9 0 4 1   arr = [-4, 3, -9, 0, 4, 1]

Sample Output

0.500000
0.333333
0.166667

Explanation 

There are 3 positive numbers, 2 negative numbers, and 1 zero in the array.

The proportions of occurrence are positive: 3/6, negative: 2/6 and zeros: 1/6.

Solution - Plus Minus - Hacker Rank Solution

Python 3 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the 'plusMinus' function below.
# The function accepts INTEGER_ARRAY arr as parameter.

def plusMinus(arr):
    positiveCounter = 0
    negetiveCounter = 0
    zeroCounter = 0

    for i in range (len(arr)):
        if arr[i] > 0:
            positiveCounter += 1
        elif arr[i] < 0 :
            negetiveCounter +=1
        else:
            zeroCounter += 1

    print("%f"%(positiveCounter / len(arr)))
    print("%f"%(negetiveCounter / len(arr)))
    print("%f"%(zeroCounter / len(arr)))
    # Write your code here

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

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

    plusMinus(arr)

Disclaimer: The above Problem (Plus Minus) is generated by Hacker Rank but the Solution is provided by Sloth Coders. This tutorial is only for Educational and Learning Purpose.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

1 comment

  1. at run time it comes name error positioncounter is not defined.