We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def plusMinus(arr):
n = len(arr) # Get the number of elements
count_positive = sum(1 for x in arr if x > 0) # Count positive numbers
count_negative = sum(1 for x in arr if x < 0) # Count negative numbers
count_zero = sum(1 for x in arr if x == 0) # Count zeros
# Calculate ratios
positive_ratio = count_positive / n
negative_ratio = count_negative / n
zero_ratio = count_zero / n
# Print results with 6 decimal places
print(f"{positive_ratio:.6f}")
print(f"{negative_ratio:.6f}")
print(f"{zero_ratio:.6f}")
if name == 'main':
n = int(input().strip()) # Read the number of elements
arr = list(map(int, input().rstrip().split())) # Read the array elements
plusMinus(arr) # Call the function
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Plus Minus
You are viewing a single comment's thread. Return to all comments →
def plusMinus(arr): n = len(arr) # Get the number of elements count_positive = sum(1 for x in arr if x > 0) # Count positive numbers count_negative = sum(1 for x in arr if x < 0) # Count negative numbers count_zero = sum(1 for x in arr if x == 0) # Count zeros
if name == 'main': n = int(input().strip()) # Read the number of elements arr = list(map(int, input().rstrip().split())) # Read the array elements