Skip to content

Instantly share code, notes, and snippets.

@fcojperez
Last active July 3, 2017 18:43
Show Gist options
  • Save fcojperez/3cef2036772a2b09cc61daefca981a90 to your computer and use it in GitHub Desktop.
Save fcojperez/3cef2036772a2b09cc61daefca981a90 to your computer and use it in GitHub Desktop.
#!/bin/python3
'''
Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. 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.
https://www.hackerrank.com/challenges/plus-minus
'''
import sys
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
print('{0:.6f}'.format(round(len([x for x in arr if x >0])/n,6))) #positives
print('{0:.6f}'.format(round(len([x for x in arr if x <0])/n,6))) #negatives
print('{0:.6f}'.format(round(len([x for x in arr if x ==0])/n,6))) #zeros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment