Robbie Hatley's Solutions To The Weekly Challenge #240

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 (2023-10-22 through 2023-10-28) is weekly challenge #240.

Task 240-1 is as follows:

Task 1: Acronym
Submitted by: Mohammad S Anwar
You are given an array of strings and a check string. Write a
script to find out if the check string is the acronym of the
words in the given array.

Example 1:
Input: @str = ("Perl", "Python", "Pascal"); $chk = "ppp";
Output: true

Example 2:
Input: @str = ("Perl", "Raku"); $chk = "rp";
Output: false

Example 3:
Input: @str = ("Oracle", "Awk", "C"); $chk = "oac";
Output: true

This is just a matter of making these two subs:

  1. sub acronym (@list) {return join('', map {substr $_, 0, 1} @list)}
  2. sub ncscomp ($x,$y) {return (fc($x) eq fc($y))}

Note: Though the official problem statement doesn't say so, the examples make it clear that either the check is compared to the lc or fc of the acronym, or the comparison is non-case sensitive. I elect NCS comparison. Here's the script I ended up with:

Robbie Hatley's Solution to The Weekly Challenge 240-1

Task 240-2 is as follows:

Task 2: Build Array
Submitted by: Mohammad S Anwar
You are given an array of integers. Write a script to create an
array such that new[i] = old[old[i]] where 0 <= i < new.length.

Example 1:
Input: @int = (0, 2, 1, 5, 3, 4)
Output: (0, 1, 2, 4, 5, 3)

Example 2:
Input: @int = (5, 0, 1, 2, 3, 4)
Output: (4, 5, 0, 1, 2, 3)

The official problem description has two problems:

Firstly, it implies that the elements of @old may be any integers, but this is not so: for $old[$old[$i]] to make sense, all array elements must be non-negative integers in the range 0..$#old. Therefore, I'll elect to run a sanity check to make sure all elements of @old are in the range 0..$#old.

Secondly, it doesn't specify scalar(@new), so this could be any integer from 0 through scalar(@old). Therefore I'll elect to set $#new to $#old. (I could just return an empty array @new = () as the "solution" for every @old array, and it would technically be valid, because $new[$i] would be equal to $old[$old[$i]] for every index $i of @new, because there are no indexes $i of @new, because @new is empty. But that's like pointing to an empty room and saying "all sheep in this room are blue", which would always be true, because all zero of those sheep are blue. 🙂 That's technically true, but its' cheating, so I won't do that.)

Those two issues being settled, for an array @old of n ints (each in the range 0..$#old), generating @new is just a matter of saying:

for (my $i = 0;$i<=$#old;++$i){$new[$i]=$old[$old[$i]]}

Here's the script I made for this:

Robbie Hatley's Solution to The Weekly Challenge 240-2

That's it for 240; see you on 241!

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