
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Numbers with Even Number of Digits in Python
Suppose we have a list of numbers. We have to count the numbers that has even number of digit count. So if the array is like [12,345,2,6,7896], the output will be 2, as 12 and 7896 has even number of digits
To solve this, we will follow these steps −
- Take the list and convert each integer into string
- if the length of string is even, then increase count and finally return the count value
Example
Let us see the following implementation to get better understanding −
class Solution(object): def findNumbers(self, nums): str_num = map(str, nums) count = 0 for s in str_num: if len(s) % 2 == 0: count += 1 return count ob1 = Solution() print(ob1.findNumbers([12,345,2,6,7897]))
Input
[12,345,2,6,7897]
Output
2
Advertisements