The weekly challenge 258 - Task 1: Count Even Digits Number
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-258/#TASK1 3 # 4 # Task 1: Count Even Digits Number 5 # ================================ 6 # 7 # You are given a array of positive integers, @ints. 8 # 9 # Write a script to find out how many integers have even number of digits. 10 # 11 ## Example 1 12 ## 13 ## Input: @ints = (10, 1, 111, 24, 1000) 14 ## Output: 3 15 ## 16 ## There are 3 integers having even digits i.e. 10, 24 and 1000. 17 # 18 ## Example 2 19 ## 20 ## Input: @ints = (111, 1, 11111) 21 ## Output: 0 22 # 23 ## Example 3 24 ## 25 ## Input: @ints = (2, 8, 1024, 256) 26 ## Output: 1 27 # 28 ############################################################ 29 ## 30 ## discussion 31 ## 32 ############################################################ 33 # 34 # Just calculate the length of each integer an check if it is 35 # even, then in the end tell how many those were. 36 37 use strict; 38 use warnings; 39 40 count_even_digits_number(10, 1, 111, 24, 1000); 41 count_even_digits_number(111, 1, 11111); 42 count_even_digits_number(2, 8, 1024, 256); 43 44 sub count_even_digits_number { 45 my @ints = @_; 46 print "Input: (" . join(", ", @ints) . ")\n"; 47 my $result = 0; 48 foreach my $number (@ints) { 49 if(length($number) % 2 == 0) { 50 $result++; 51 } 52 } 53 print "Output: $result\n"; 54 } 55 56