Peter
Peter Campbell Smith

Middle aged and oldies

Weekly challenge 231 — 21 August 2023

Week 231 - 21 Aug 2023

Task 2

Task — 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).

Examples


Example 1
Input: @list = ("7868190130M7522","5303914400F9211",
	"9273338290F4010")
Output: 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")
Output: 0

Analysis

I think this is the shortest solution I've come up with since I started doing these challenges. The answer is:
grep(/^.{11}[6789]/, @_).

The pattern matches anything with 6, 7, 8 or 9 as its 12th character and conveniently say takes the output of the grep in scalar context, so that the grep returns a count of the input items that match the pattern.

Try it 

Try running the script with any input, for example:
7868190130M7522, 5303914400F9211, 9273338290F4010


Script


#!/usr/bin/perl

use v5.16;    # The Weekly Challenge - 2023-08-21
use utf8;     # Week 231 task 2 - Senior citizens
use strict;   # Peter Campbell Smith
use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

senior_citizens('7868190130M7522','5303914400F9211','9273338290F4010');
senior_citizens('1313579440F2036','2921522980M5644');
senior_citizens('0000000000M5900', '0000000000M6000', '0000000000M6100', '0000000000M6200');

sub senior_citizens {
    
    say qq[\nInput: (] . join(', ', @_) . ')';
    say qq[Output: ] . grep(/^.{11}[6789]/, @_);
}

Output


Input: (7868190130M7522, 5303914400F9211, 9273338290F4010)
Output: 2

Input: (1313579440F2036, 2921522980M5644)
Output: 0

Input: (0000000000M5900, 0000000000M6000, 0000000000M6100,
	0000000000M6200)
Output: 3

The content of this website which has been created by
Peter Campbell Smith is hereby placed in the public domain