- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to count number of elements in a list that contains odd number of digits in Python
Suppose we have a list of positive numbers called nums, we have to find the number of elements that have odd number of digits.
So, if the input is like [1, 300, 12, 10, 3, 51236, 1245], then the output will be 4
To solve this, we will follow these steps −
- c:= 0
- for i in range 0 to size of nums, do
- s:= digit count of nums[i]
- if s is odd, then
- c:= c+1
- return c
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): c=0 for i in range(len(nums)): s=len(str(nums[i])) if s%2!=0: c=c+1 return c ob = Solution() print(ob.solve([1, 300, 12, 10, 3, 51236, 1245]))
Input
[1, 300, 12, 10, 3, 51236, 1245]
Output
4
Advertisements