0 out of 464 challenges solved
Write a function `zero_to_nonzero_ratio` that calculates the ratio of zeroes to non-zeroes in a given list of integers. The function should return the ratio as a floating-point number. If the list contains no non-zero elements, return `float('inf')` to indicate an infinite ratio. #### Example Usage ```python [main.nopy] print(zero_to_nonzero_ratio([0, 1, 2, 0, 3])) # Output: 0.6666666666666666 print(zero_to_nonzero_ratio([1, 2, 3])) # Output: 0.0 print(zero_to_nonzero_ratio([0, 0, 0])) # Output: inf ```
def zero_to_nonzero_ratio(nums): """ Calculate the ratio of zeroes to non-zeroes in a list of integers. Args: nums (list): A list of integers. Returns: float: The ratio of zeroes to non-zeroes, or float('inf') if no non-zeroes exist. """ # Count the number of zeroes and non-zeroes zero_count = 0 nonzero_count = 0 # Iterate through the list to count zeroes and non-zeroes for num in nums: if num == 0: zero_count += 1 else: nonzero_count += 1 # Calculate and return the ratio # Placeholder for the solution pass