Posts

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

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-04-16 through 2023-04-22) is weekly challenge #213. Task 1 is as follows: "Write a script to sort a list of positive integers so that the sorted list consists of the even integers in ascending order followed by the odd integers in ascending order." I found this super-simple: Robbie Hatley's Perl Solution to The Weekly Challenge 213-1 Task 2 is as follows: You are given a list of bidirectional routes defining a network of nodes, as well as source and destination node numbers. Write a script to find the route from source to destination that passes through fewest nodes. That one, I didn't have time to complete, but here's a stub, as far as I got with it: Robbie Hatley's Perl Solution to The Weekly Challenge 212-2 That's it for 213; see you...

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

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-04-09 through 2023-04-15) is weekly challenge #212. Task 1 is as follows: You are given a word having alphabetic characters only, and a list of positive integers of the same length. Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word. This is like a Caesar cipher but with the added kink that every index within the plaintext gets rotated an independent amount. I found it very straightforward to program, just a matter of doing chr(65 + (ord($a)-65+$j)%26) for uppercase and chr(97 + (ord($a)-97+$j)%26) for lowercase: Robbie Hatley's Perl Solution to The Weekly Challenge 212-1 Task 2 is as follows: You are given a...

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

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-04-02 through 2023-04-08) is weekly challenge #211. Task 1 is as follows: You are given a matrix m x n. Write a script to find out if the given matrix is Toeplitz Matrix. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements. For some reason this gave me more trouble than Task 2 below even though that one was conceptually harder. I guess I just found it annoying that I had to riffle through columns of all different lengths. My approach turned out to use... can you guess?... yep, nested 3-part loops again. :-) I do tend to use those a lot. Check it out: Robbie Hatley's Perl Solution to The Weekly Challenge 211-1 Task 2 is as follows: You are given an array of integers. Write a script to find out if the given can be split into two se...

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...