Programming challenges

The Weekly Challenge - 264 (April 8th 2024)

Task #1: Greatest English Letter

You are given a string, $str, made up of only alphabetic characters [a..zA..Z].

Write a script to return the greatest english letter in the given string.

A letter is greatest if it occurs as lower and upper case. Also letter ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.

Example 1

Input:  'PeRlwEeKLy'
Output: L

There are two letters E and L that appears as lower and upper.
The letter L appears after E, so the L is the greatest english letter.

Example 2

Input: 'ChaLlenge'
Output: L

Example 3

Input: 'The'
Output: ''

Solution

We first need to collect which letters we have seen in both upper and lower case.

We create a hash, and for each letter we logical or it with 1/2 when we see the letter in upper/lower case.

If we see both this means the value is 3.

We then filter the letters – and then sort them – returning the last value of this list – which will be the greatest or undefined.

challenge-264-task-1.pl - perl

sub greatest_english_letter {
my %letters;
$letters{ uc $_ } |= $_ gt 'Z' ? 2 : 1 for split //, pop;
( sort grep { $letters{$_} == 3 } keys %letters )[-1]
}

Note:

Note if you want to truly return ” rather than “undef” in the case there are no letters that are both upper case you can add ”, to the last line.

perl

sub greatest_english_letter {
my %letters;
$letters{ uc $_ } |= $_ gt 'Z' ? 2 : 1 for split //, pop;
( '', sort grep { $letters{$_} == 3 } keys %letters )[-1]
}

Task #2: Target Array

You are given two arrays of integers, @source and @indices. The @indices can only contains integers 0 <= i < size of @source.

Write a script to create target array by insert at index $indices[i] the value $source[i].

Example 1

Input: @source  = (0, 1, 2, 3, 4)
       @indices = (0, 1, 2, 2, 1)
Output: (0, 4, 1, 3, 2)
@source  @indices  @target
0        0         (0)
1        1         (0, 1)
2        2         (0, 1, 2)
3        2         (0, 1, 3, 2)
4        1         (0, 4, 1, 3, 2)

Example 2

Input: @source  = (1, 2, 3, 4, 0)
       @indices = (0, 1, 2, 3, 0)
Output: (0, 1, 2, 3, 4)
@source  @indices  @target
1        0         (1)
2        1         (1, 2)
3        2         (1, 2, 3)
4        3         (1, 2, 3, 4)
0        0         (0, 1, 2, 3, 4)

Example 3

Input: @source  = (1)
       @indices = (0)
Output: (1)

Solution

Although this looks harder – in Perl this is “logically” easier, as all you need to do is repeatedly splice the value into the right index in the array.

challenge-264-part-2.pl - perl

sub target_array {
my( @indicies, @result ) = @{$_[1]};
splice @result, shift @indicies, 0, $_ for @{$_[0]};
@result
}

Using List::Util

List::Util has a method zip which takes two arrayrefs and returns an array of arrayref pairs – effectively transposing the matrix…

So

zip( [1,2,3], [4,5,6] )

returns

( [1,4], [2,5], [3,6] )

this removes the need for extra variables and leads to a neater loop.

challenge-264-part-2-v2.pl - perl

use List::Util qw(zip);

sub target_array_zip {
my @result;
splice @result, $_->[1], 0, $_->[0] for zip @_;
@result
}

About The Weekly Challenge

The weekly challenge was set up Mohammad Anwar approximately 5 years ago - each Monday two problems are set for the "team" to solve.

These are posted at: