Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count number of elements in a list that contains odd number of digits in Python
Given a list of positive numbers, we need to count how many elements have an odd number of digits. For example, the number 123 has 3 digits (odd), while 1234 has 4 digits (even).
So, if the input is like [1, 300, 12, 10, 3, 51236, 1245], then the output will be 4 because numbers 1, 3, 300, and 1245 have odd digit counts (1, 1, 3, and 4 digits respectively).
Algorithm
To solve this, we will follow these steps:
- Initialize counter c = 0
- For each number in the list:
- Count the digits by converting to string and finding length
- If digit count is odd, increment counter
- Return the counter
Method 1: Using String Conversion
Convert each number to string and check the length ?
def count_odd_digits(nums):
count = 0
for num in nums:
digit_count = len(str(num))
if digit_count % 2 != 0:
count += 1
return count
# Test the function
numbers = [1, 300, 12, 10, 3, 51236, 1245]
result = count_odd_digits(numbers)
print(f"Numbers with odd digits: {result}")
# Let's see which numbers have odd digits
for num in numbers:
digits = len(str(num))
print(f"{num} has {digits} digits ({'odd' if digits % 2 != 0 else 'even'})")
Numbers with odd digits: 4 1 has 1 digits (odd) 300 has 3 digits (odd) 12 has 2 digits (even) 10 has 2 digits (even) 3 has 1 digits (odd) 51236 has 5 digits (odd) 1245 has 4 digits (even)
Method 2: Using Math Operations
Count digits using mathematical operations instead of string conversion ?
import math
def count_digits_math(num):
if num == 0:
return 1
return int(math.log10(num)) + 1
def count_odd_digits_math(nums):
count = 0
for num in nums:
digit_count = count_digits_math(num)
if digit_count % 2 != 0:
count += 1
return count
# Test the function
numbers = [1, 300, 12, 10, 3, 51236, 1245]
result = count_odd_digits_math(numbers)
print(f"Numbers with odd digits: {result}")
Numbers with odd digits: 4
Method 3: Using List Comprehension
A more concise approach using list comprehension ?
def count_odd_digits_compact(nums):
return sum(1 for num in nums if len(str(num)) % 2 != 0)
# Test the function
numbers = [1, 300, 12, 10, 3, 51236, 1245]
result = count_odd_digits_compact(numbers)
print(f"Numbers with odd digits: {result}")
# Alternative using list comprehension
odd_digit_numbers = [num for num in numbers if len(str(num)) % 2 != 0]
print(f"Actual numbers with odd digits: {odd_digit_numbers}")
print(f"Count: {len(odd_digit_numbers)}")
Numbers with odd digits: 4 Actual numbers with odd digits: [1, 300, 3, 51236] Count: 4
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| String Conversion | O(n × d) | O(d) | High |
| Math Operations | O(n × log d) | O(1) | Medium |
| List Comprehension | O(n × d) | O(1) | High |
Where n = number of elements, d = average number of digits
Conclusion
The string conversion method is most readable and commonly used. For better performance with large numbers, use the mathematical approach. List comprehension provides the most concise solution.
