Robbie Hatley's Solutions To The Weekly Challenge #264

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2024-04-07 through 2024-04-13) is weekly challenge #264. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 264-1: Greatest English Letter
Submitted by: Mohammad Sajid Anwar
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: $str = '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: $str = 'ChaLlenge'
Output: L

Example 3:
Input: $str = 'The'
Output: ''

My approach to solving this problem will be:

  1. Make a list "@chars" of all characters from the given input string "$str".
  2. Iterate through that list from left to right, keeping track of the "greatest" character found so-far which appears as both lower-case and UPPER-CASE in $str.
  3. Return the greatest character found (or return an empty string if no great characters were found).
   sub gel ($str) {
      my @chars = split //, $str;
      my $greatest = '';
      foreach my $char (@chars) {
         if ( any {$_ eq ($char =~ y/A-Za-z/a-zA-Z/r) } @chars ) {
            if ( uc($char) gt $greatest ) {
               $greatest = uc($char);
            }
         }
      }
      return $greatest;
   }

Robbie Hatley's Perl Solution to The Weekly Challenge 264-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 264-2: Target Array
Submitted by: Mohammad Sajid Anwar
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 inserting 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)

This is one of those few Weekly Challenges where Task 2 is simpler than Task 1 (usually it's the other way around). I'll solve this problem by using Perl's built-in "splice" feature to "splice" desired elements from the "source" array into the target array being constructed, using the indices from the "indices" array to specify insertion points.

   sub target_array ($s, $i) {
      my @t = ();
      for ( my $ii=0 ; $ii <= $#$s ; ++$ii ) {
         splice @t, $$i[$ii], 0, $$s[$ii];
      }
      return @t;
   }

Robbie Hatley's Perl Solution to The Weekly Challenge 264-2

That's it for challenge 264; see you on challenge 265!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273