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
Selected Reading
Find Numbers with Even Number of Digits in Python
Given a list of numbers, we need to count how many numbers have an even number of digits. For example, if the array is [12, 345, 2, 6, 7896], the output will be 2, since 12 (2 digits) and 7896 (4 digits) have even digit counts.
Approach
To solve this problem, we will follow these steps ?
- Convert each integer to a string to easily count digits
- Check if the length of the string is even using modulo operation
- Increment counter for numbers with even digit count
- Return the final count
Using Class-based Solution
Here's the implementation using a class method ?
class Solution:
def findNumbers(self, nums):
count = 0
for num in nums:
# Convert to string and check if length is even
if len(str(num)) % 2 == 0:
count += 1
return count
# Test the solution
ob1 = Solution()
result = ob1.findNumbers([12, 345, 2, 6, 7896])
print(f"Numbers with even digits: {result}")
Numbers with even digits: 2
Using Simple Function
A more straightforward approach without using a class ?
def count_even_digits(numbers):
count = 0
for num in numbers:
if len(str(num)) % 2 == 0:
count += 1
return count
# Test with different examples
test_cases = [
[12, 345, 2, 6, 7896],
[555, 901, 482, 1771],
[1, 22, 333, 4444, 55555]
]
for i, nums in enumerate(test_cases, 1):
result = count_even_digits(nums)
print(f"Test case {i}: {nums}")
print(f"Count of numbers with even digits: {result}")
print()
Test case 1: [12, 345, 2, 6, 7896] Count of numbers with even digits: 2 Test case 2: [555, 901, 482, 1771] Count of numbers with even digits: 3 Test case 3: [1, 22, 333, 4444, 55555] Count of numbers with even digits: 2
Using List Comprehension
A more concise solution using list comprehension ?
def count_even_digits_compact(numbers):
return sum(1 for num in numbers if len(str(num)) % 2 == 0)
# Test the compact version
numbers = [12, 345, 2, 6, 7896]
result = count_even_digits_compact(numbers)
print(f"Input: {numbers}")
print(f"Numbers with even digits: {result}")
# Show which numbers have even digits
even_digit_nums = [num for num in numbers if len(str(num)) % 2 == 0]
print(f"The numbers with even digits are: {even_digit_nums}")
Input: [12, 345, 2, 6, 7896] Numbers with even digits: 2 The numbers with even digits are: [12, 7896]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Class-based | Good | Standard | Object-oriented solutions |
| Simple function | Excellent | Standard | General use cases |
| List comprehension | Good | Slightly better | Concise code |
Conclusion
To count numbers with even digits, convert each number to a string and check if its length is even. The list comprehension approach provides the most concise solution, while the simple function offers the best readability.
Advertisements
