THE WEEKLY CHALLENGE - 231

https://theweeklychallenge.org/blog/perl-weekly-challenge-231/

Table of Contents

Task 1 - Min Max

A common coding interview question is to find all the numbers in a list that are neither the minimum nor maximum value. This "Min Max" problem tests your ability to iterate through a list, track minimum and maximum values, and build a result list.

Let's look at example solutions in Python and Perl.

Python Solution

Here is one way to solve this in Python:

def find_neither_min_max(ints):
  min_val, max_val = ints[0], ints[0]

  for num in ints:
    if num < min_val:
      min_val = num
    elif num > max_val:
      max_val = num

  result = [num for num in ints if num not in (min_val, max_val)]

  return result if result else -1

We start by initializing min_val and max_val to the first element of the list. Then we loop through the list, updating min_val and max_val if we find a smaller or larger value respectively.

After finding min and max, we build a result list by including only numbers that are not equal to min_val or max_val. We return the result list or -1 if no numbers exist between min and max.

Perl Solution

Here is an implementation in Perl:

sub find_neither_min_max {
  my @ints = @_;

  my ($min, $max) = (sort {$a <=> $b} @ints)[0,-1];

  my @result;

  foreach my $num (@ints) {
    if ($num != $min && $num != $max) {
      push @result, $num; 
    }
  }

  return @result ? @result : -1;
}

The logic is similar - we sort to find min and max, then build a result list excluding those values.

The Perl version uses sorting rather than iterating to find min and max. And it builds the result array by pushing qualifying elements onto it.

Key Takeaways

Some key techniques for the Min Max problem:

  • Initialize min/max variables before iterating
  • Update min/max if finding lower/higher values
  • Build result list and exclude min/max values
  • Return result list or sentinel value if no middle elements

Mastering iteration, tracking state, and building results are essential coding skills. Min Max is a good test of these fundamentals.


Task 2 - Senior Citizens

Introduction

Senior citizens are an important demographic group that often require special accommodations and services. In the context of travel, senior citizens may be eligible for discounts, preferential seating, and other benefits. In this blog post, I will explore how to count the number of senior citizens in a list of passenger details using Perl and Python.

Problem Statement

We 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. Our task is to count the number of senior citizens (age >= 60) in the list.

Perl Solution

To solve this problem in Perl, we can use the following solution:

sub count_senior_citizens {
    my @passengers = @_;
    my $count = grep { $_ >= 60 } map { /(\d{10}[MF])(\d{2})/; $2 } @passengers;
    return $count;
}

In this solution, the count_senior_citizens function takes a list of passenger details as input and returns the count of senior citizens. The function uses a regular expression to extract the age from each passenger detail and checks if the age is greater than or equal to 60. The sum of the boolean values is then returned as the count of senior citizens.

Python Solution

To solve this problem in Python, we can use the following solution:

from typing import List

def count_senior_citizens(passengers: List[str]) -> int:
    """
    Count the number of senior citizens in a list of passenger details.

    Args:
        passengers: 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.

    Returns:
        The count of all senior citizens (age >= 60).
    """
    return sum(int(passenger[11:13]) >= 60 for passenger in passengers)

In this solution, the count_senior_citizens function takes a list of passenger details as input and returns the count of senior citizens. The function uses a list comprehension to iterate over the passenger details, extract the age from each detail, and check if the age is greater than or equal to 60. The sum of the boolean values is then returned as the count of senior citizens.

Testing and Documentation

To test the solutions, we can use the Test::More module in Perl and the unittest module in Python. We can also add docstrings and type annotations to the functions to provide a clear description of their purpose, arguments, and return value.