The weekly challenge 231 - Task 2: Senior Citizens
1 #!/usr/bin/perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-231/#TASK2 3 # 4 # Task 2: Senior Citizens 5 # ======================= 6 # 7 # You are given a list of passenger details in the form “9999999999A1122”, where 9 denotes the phone number, A the sex, 1 the age and 2 the seat number. 8 # 9 # Write a script to return the count of all senior citizens (age >= 60). 10 # 11 ## Example 1 12 ## 13 ## Input: @list = ("7868190130M7522","5303914400F9211","9273338290F4010") 14 ## Ouput: 2 15 ## 16 ## The age of the passengers in the given list are 75, 92 and 40. 17 ## So we have only 2 senior citizens. 18 # 19 ## Example 2 20 ## 21 ## Input: @list = ("1313579440F2036","2921522980M5644") 22 ## Ouput: 0 23 # 24 ############################################################ 25 ## 26 ## discussion 27 ## 28 ############################################################ 29 # 30 # The data format has a few problems here. Not only is the 31 # telephone number fixed length, the same is true for the age 32 # (I guess people are not allowed to be older than 99 years here) 33 # and the seat number. 34 # In our case, we're only interested in the age really, so we 35 # can pick the last four characters, from which we pick the first 36 # two characters. 37 38 senior_citizens("7868190130M7522","5303914400F9211","9273338290F4010"); 39 senior_citizens("1313579440F2036","2921522980M5644"); 40 41 sub senior_citizens { 42 my @list = @_; 43 print "Input: (" . join(", ", @list) . ")\n"; 44 my $seniors = 0; 45 foreach my $elem (@list) { 46 my ($tel, $age_seat) = split /[MF]/, $elem; 47 my $age = substr($age_seat, 0, 2); 48 $seniors++ if $age >= 60; 49 } 50 print "Output: $seniors\n"; 51 }