Perl Weekly Challenge 231: Senior Citizens

These are some answers to the Week 231, Task 2, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on August 27, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Senior Citizens

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.

Write a script to return the count of all senior citizens (age >= 60).

Example 1

Input: @list = ("7868190130M7522","5303914400F9211","9273338290F4010")
Ouput: 2

The age of the passengers in the given list are 75, 92 and 40.
So we have only 2 senior citizens.

Example 2

Input: @list = ("1313579440F2036","2921522980M5644")
Ouput: 0

This is a very simple task, which can easily be solved with a one-liner solution. The only slight difficulty might be to deal with passengers aged 100 and above, but we'll have to ignore this edge case since we have not been given any information about them.

Since the input test items are rather long, my solutions will display only the last four digits of each data item, preceded by an ellipsis (...), in order to avoid silly formatting problems in the output as presented in this blog post.

Senior Citizens in Raku

The count-seniors subroutine uses the built-in substr method to retrieve the two digits containing the passenger's age and compare it with the age limit.

sub count-seniors (@in) {
    return (grep { .substr(11, 2) >= 60 }, @in).elems;
}
for <7868190130M7522 5303914400F9211 9273338290F4010>,
    <1313579440F2036 2921522980M5644> -> @test {
    printf "...%-s  ", .substr(11, 4) for  @test;
    say " => ", count-seniors @test;
}

This program displays the following output:

$ raku ./senior-citizens.raku
...7522  ...9211  ...4010   => 2
...2036  ...5644   => 0

Senior Citizens in Perl

This is a port to Perl of the above Raku program, using the same built-in substr function.

use strict;
use warnings;
use feature 'say';

sub count_seniors {
    return scalar (grep { substr($_, 11, 2) >= 60 } @_);
}
for my $test 
    ([<7868190130M7522 5303914400F9211 9273338290F4010>],
     [<1313579440F2036 2921522980M5644>]) {
    printf "...%-s  ", substr($_, 11, 4) for @$test;
    say " => ", count_seniors @$test;
}

This program displays the following output:

$ perl ./senior-citizens.pl
...7522  ...9211  ...4010   => 2
...2036  ...5644   => 0

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on September 3, 2023. And, please, also spread the word about the Perl Weekly Challenge if you can.

2 Comments

This challenge only requested the count, right? It could have been even more simple if you wanted.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.