375,936 views
4 votes
4 votes
Write a function named digit_count that takes one parameter that is a number (int or float) and returns a count of even digits, a count of odd digits, and a count of zeros that are to the left of the decimal point. Return the three counts in that order: even count, odd count, zero count. For example: digit_count(1234567890123) returns (5, 7, 1) digit_count(123400. 345) returns (2, 2, 2) digit_count(123. ) returns (1, 2, 0) digit_count(0. 123) returns (0, 0, 1)

User Ritika
by
2.5k points

1 Answer

11 votes
11 votes

def digit_count(num):

# Convert the number to a string and split it at the decimal point

num_str = str(num).split(".")

# Initialize variables to keep track of the counts

even_count = 0

odd_count = 0

zero_count = 0

# Iterate through the digits to the left of the decimal point

for ch in num_str[0]:

if ch == "0":

zero_count += 1

elif int(ch) % 2 == 0:

even_count += 1

else:

odd_count += 1

# Return the counts in the order: even count, odd count, zero count

return (even_count, odd_count, zero_count)

User Kaushik Vatsa
by
2.6k points