1
+ ##==================================
2
+ ## Leetcode
3
+ ## Student: Vandit Jyotindra Gajjar
4
+ ## Year: 2020
5
+ ## Problem: 1295
6
+ ## Problem Name: Find Numbers with Even Number of Digits
7
+ ##===================================
8
+ #
9
+ #Given an array nums of integers, return how many of them contain an even number of digits.
10
+ #
11
+ #Example 1:
12
+ #
13
+ #Input: nums = [12,345,2,6,7896]
14
+ #Output: 2
15
+ #Explanation:
16
+ #12 contains 2 digits (even number of digits).
17
+ #345 contains 3 digits (odd number of digits).
18
+ #2 contains 1 digit (odd number of digits).
19
+ #6 contains 1 digit (odd number of digits).
20
+ #7896 contains 4 digits (even number of digits).
21
+ #Therefore only 12 and 7896 contain an even number of digits.
22
+ #
23
+ #Example 2:
24
+ #
25
+ #Input: nums = [555,901,482,1771]
26
+ #Output: 1
27
+ #Explanation:
28
+ #Only 1771 contains an even number of digits.
29
+ class Solution :
30
+ def findNumbers (self , array ):
31
+ count = 0 #Intialize count
32
+ for i in range (len (array )): #Loop through array
33
+ #print(array[i])
34
+ splitArrayDigit = [int (j ) for j in str (array [i ])] #Split the array's number into each digit.
35
+ #print(splitArrayDigit)
36
+ if len (splitArrayDigit ) % 2 == 0 : #Condition-check: If array's digits are even we'll enter the if condition
37
+ count += 1 #Update the counter by 1
38
+ return count #We'll return the counter at the end of loop.
39
+
40
+ #Example:
41
+ #array = [555, 901, 482, 1771]
42
+ #Count = 0
43
+ #i = 0
44
+ #splitArrayDigit = [5, 5, 5]
45
+ #If condition-check is false for i = 0.
46
+ #Count = 0
47
+ #i = 1
48
+ #splitArrayDigit = [9, 0, 1]
49
+ #If condition-check is false for i = 1.
50
+ #Count = 0
51
+ #i = 2
52
+ #splitArrayDigit = [4, 8, 2]
53
+ #If condition-check is false for i = 2.
54
+ #Count = 0
55
+ #i = 3
56
+ #splitArrayDigit = [1, 7, 7, 1]
57
+ #If condition-check is true for i = 3.
58
+ #Count = 1
59
+ #So for this array we'll get count = 1. So even number of digits are for this example is 1.
0 commit comments