Robbie Hatley's Perl Solutions To The Weekly Challenge #203

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

The Weekly Challenge

This week (2023-02-05 through 2023-02-11) is weekly challenge #203.

Task 1 I found absurdly simple:

Write a script to determine the number of "Special Quadruplets" in any given array of integers. Given an array "nums" of integers, a "Special Quadruplet" is a slice nums[a,b,c,d] that satisfies the following 2 rules:
1) nums[a] + nums[b] + nums[c] == nums[d]
2) a < b < c < d

That had "quadruple nested for loops" written all over it, so that's just what I did:

for (@arrays){
   my @nums  = @{$_};
   my @quads = ();
   my ($a, $b, $c, $d) = (0,0,0,0);
   for (          $a =    0   ; $a <= $#nums - 3 ; ++$a ){
      for (       $b = $a + 1 ; $b <= $#nums - 2 ; ++$b ){
         for (    $c = $b + 1 ; $c <= $#nums - 1 ; ++$c ){
            for ( $d = $c + 1 ; $d <= $#nums - 0 ; ++$d ){
               if ( $nums[$a] + $nums[$b] + $nums[$c] == $nums[$d] ){
                  push @quads, [$a, $b, $c, $d];}}}}}
   my $nq = scalar @quads;
   say '';
   say "\@nums = (@nums)";
   say "number of Special Quadruplets = $nq :";
   say "\@nums[@{$_}] = (@nums[@{$_}])" for @quads;}

Task 2, however, I found much more complex: "You are given path to two folders, $source and $target. Write a script that recursively copy the directory structure from $source to $target, but not any files." This would probably have been beyond my ability to complete within 1 week if it had not been for the fact that I've written many similar programs involving directory-tree-walking before. So I grabbed some existing subs from my personal modules, tossed them in a single file, and whipped it into shape. This is by far the longest script I've written for PWCC, at over 400 lines; but it's a fully-functional directory-tree cloner, with full-Unicode ability (handles dir names in Russian, Bengali, Inuktitut, or whatever), and with lots of error checking: My code is too lengthy to cut-n-paste to this blog, but see here:

Robbie's Solution to 203-2

That's it for 203; looking forward to 204.

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239