DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Algorithms Problem Solving: Even Number of Digits
TK
TK

Posted on • Originally published at leandrotk.github.io

Algorithms Problem Solving: Even Number of Digits

This post is part of the Algorithms Problem Solving series.

Problem description

This is the Find Numbers with Even Number of Digits problem. The description looks like this:

Given an array nums of integers, return how many of them contain an even number of digits.

Examples

Input: nums = [12,345,2,6,7896]
Output: 2

Input: nums = [555,901,482,1771]
Output: 1
Enter fullscreen mode Exit fullscreen mode

Solution

The solution idea is to iterate through the numbers list and for each number, verify how many digits it has and if it has an even digits number, increment the counter.

Using Python3, we can transform the number into a string and get the length of it. If it is divisible by 2, it is an even number of digits.

def find_numbers(nums):
    counter = 0

    for num in nums:
        if len(str(num)) % 2 == 0:
            counter += 1

    return counter
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: