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

 Live Demo

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

Updated on: 06-Oct-2020

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements