Posts

Showing posts from March, 2023

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

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-03-26 through 2023-04-01) is weekly challenge #210. Task 1 is as follows: Write a script to get the maximum "points" possible from "taking out" or "killing" (removing) integers from a list. For each integer you remove, all integers exactly one-less or one-more will also be removed. The "points" will be the total of integers removed. What integers should you remove to get maximum points? That's pretty straightforward as long as only positive integers are involved, but it gets a bit trickier if 0s or negative integers are present. For max score, one should manually remove all positive integers, and only positive integers, never 0s (might reduce score) or negative integers (ALWAYS reduces score). As with many of my solutions, I used

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

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-03-19 through 2023-03-25) is weekly challenge #209. Task 1 is as follows: You are given an array of binary bits that ends with 0. Valid sequences in the bit string are: [0] -decodes-to-> "a" [1, 0] -> "b" [1, 1] -> "c" Write a script to print 1 if the last character is an “a”, otherwise print 0. This is perhaps the more technically-challenging of the two tasks this we, as it pretty much requires usage of a Finite State Machine. But for me that's easy, as I'm good at making finite state machines. So I did so: Robbie's Solution to TWC 209-1 Task 2 was this: Task 2: Merge Account Submitted by: Mohammad S Anwar You are given an array of accounts i.e. name with list of email addresses. Write a script to merge the accoun

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

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-03-12 through 2023-03-18) is weekly challenge #208. Task 1 is "Write a script to find all common strings between a pair of lists of string with minimum index sum. If no common strings are found, return an empty list." I found this very straightforward. Just a matter of arrays of arrays of arrays and nested for loops: Task 1 Solution Source Code Task 2 is "Write a script to return the duplicate integer and missing integer in an array which supposedly has one missing integer and one duplicate integer, or return -1 if the numbers of missing and duplicate integers are not both 1." I found this much tricker than Task 1 because of the inherent vagueness of what "missing" means, and because the answers given in Example 3 (see source below) force assumtion

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

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-03-05 through 2023-03-11) is weekly challenge #207. Task 1 is to write a script which prints all of the words in a given array that can be typed using only letters on one row of a standard QWERTY keyboard. My solution was to create two simple subroutines, one to give the keyboard row number (1, 2, or 3) of any letter, and one to determine if all the letters of a word on on the same row: use List::AllUtils 'all'; sub row($letter){ if ($letter =~ m/[qwertyuiop]/i ) {return 1} elsif ($letter =~ m/[asdfghjkl]/i ) {return 2} elsif ($letter =~ m/[zxcvbnm]/i ) {return 3} else {return 0}} sub one_row($word){ my @letters = split //,$word; my $rofl = row($letters[0]); if (all {row($_) eq $rofl} @letters) {return 1} el

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

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-26 through 2023-03-04) is weekly challenge #206. Task 1 is to write a script which returns the shortest time duration between any two times in an array of 24HR time strings. The tricky bit here was realizing that given any two time strings, the time duration between them can have two values, the smaller being 1440 minutes minus the greater, and it's the shorter duration we want in each case, then pick the shortest among all cases: for (@arrays){ my @array = @{$_}; my @times = map {60*substr($_,0,2)+substr($_,3,2)} @array; my @diffs; # elapsed times for ( my $i = 0 ; $i <= $#times - 1 ; ++$i ){ for ( my $j = $i + 1 ; $j <= $#times - 0 ; ++$j ){ my ($t1, $t2) = sort {$a<=>$b} ($times[$i], $times[$j]); my $te = $t2 - $t1