일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- AI 경진대회
- 캐치카페
- leetcode
- ubuntu
- Baekjoon
- 맥북
- 자연어처리
- 편스토랑
- 프로그래머스
- github
- programmers
- 프로그래머스 파이썬
- 데이콘
- hackerrank
- Real or Not? NLP with Disaster Tweets
- gs25
- 우분투
- 금융문자분석경진대회
- SW Expert Academy
- Kaggle
- Git
- 코로나19
- 파이썬
- ChatGPT
- 더현대서울 맛집
- 백준
- dacon
- 편스토랑 우승상품
- PYTHON
- Docker
- Today
- Total
솜씨좋은장씨
[HackerRank] Plus Minus (Python) 본문
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:
- A decimal representing of the fraction of positive numbers in the array compared to its size.
- A decimal representing of the fraction of negative numbers in the array compared to its size.
- 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)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[SW_Expert_Academy] 5688번 세제곱근을 찾아라 (Python) (0) | 2020.03.18 |
---|---|
[SW_Expert_Academy] 7701번 염라대왕의 이름 정렬 (Python) (0) | 2020.03.17 |
[HackerRank] Diagonal Difference (Python) (0) | 2020.03.15 |
[HackerRank] Dictionaries and Hashmaps: Two Strings (Python) (0) | 2020.03.14 |
[HackerRank] Arrays: Array Manipulation (Python) (0) | 2020.03.13 |