관리 메뉴

솜씨좋은장씨

[HackerRank] Plus Minus (Python) 본문

Programming/코딩 1일 1문제

[HackerRank] Plus Minus (Python)

솜씨좋은장씨 2020. 3. 16. 22:25
728x90
반응형

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.

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.

For example, given the array arr = [1, 1, 0, -1, -1 ] there are  elements, two positive, two negative and one zero. Their ratios would be 2/5 = 0.400000, 2/5 = 0.400000,  and 1/5 = 0.200000. It should be printed as

0.400000
0.400000
0.200000

Function Description

Complete the plusMinus function in the editor below. It should print out the ratio of positive, negative and zero items in the array, each on a separate line rounded to six decimals.

plusMinus has the following parameter(s):

  • arr: an array of integers

Input Format

The first line contains an integer, n, denoting the size of the array.
The second line contains n space-separated integers describing an array of numbers .

arr ( arr [0], arr [1], arr [2], ... , arr [ n - 1 ] ).

 

Constraints

0 < n <= 100

-100 <= arr [i] <= 100

 

Output Format

You must print the following 3 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 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The proportions of occurrence are positive: 3 / 6 = 0.500000, negative: 2 / 6 = 0.333333 and zeros: 1 / 6 = 0.166667.

 

Solution

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the plusMinus function below.
def plusMinus(arr):
    plus = 0
    minus = 0
    zero = 0
    for i in range(len(arr)):
        if arr[i] == 0:
            zero = zero + 1
        elif arr[i] < 0:
            minus = minus + 1
        else:
            plus = plus + 1

    len_arr = len(arr)
    minus_fraction = float(minus)/float(len_arr)
    plus_fraction = float(plus) / float(len_arr)
    zero_fraction = float(zero) / float(len_arr)
    print(plus_fraction)
    print(minus_fraction)
    print(zero_fraction)

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments