Posts

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #315 (Theme: “If I Had Words”)

Image
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 The Weekly Challenge for the week of 2025-03-31 through 2025-04-06 is #315. “If I had words To make a day for you I sing you a morning golden and new “I would make this day Last for all time Give you a night Deep in moonshine” ~~Johnathan Hodge The tasks for challenge #315 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 315-1: Find Words Submitted by: Mohammad Sajid Anwar You are given a list of words and a character. Write a script to return the index of word in the list where you find the given character. Example #1: Input: @list = ("the", "weekly", "challenge") $char = "e" Output: (0, 1, 2) Example #2: Input: @list = ("perl", "raku", "python") $char = "p" ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #314 (Theme: "Destroy Unwanted Columns")

Image
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 The Weekly Challenge for the week of 2025-03-24 through 2025-03-30 is #314. The tasks for challenge #314 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 314-1: Equal Strings Submitted by: Mohammad Sajid Anwar You are given three strings. You are allowed to remove the rightmost character of a string to make all equals. Write a script to return the number of operations to make it equal, otherwise -1. Example #1: Input: $s1 = "abc", $s2 = "abb", $s3 = "ab" Output: 2 Operation 1: Delete "c" from the string "abc" Operation 2: Delete "b" from the string "abb" Example #2: Input: $s1 = "ayz", $s2 = "cyz", $s3 = "xyz" Output: -1 Example #3: Input: $s1 = "yza", $s...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #313 ("Broken Keys" and "Reverse Letters")

Image
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 The Weekly Challenge for the week of 2025-03-17 through 2025-03-23 is #313. The tasks for challenge #313 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 313-1: Broken Keys Submitted by: Mohammad Sajid Anwar You have a broken keyboard which sometimes type a character more than once. You are given a string and actual typed string. Write a script to find out if the actual typed string is meant for the given string. Example #1: Input: $name = "perl", $typed = "perrrl" Output: true Here "r" is pressed 3 times instead of 1 time. Example #2: Input: $name = "raku", $typed = "rrakuuuu" Output: true Example #3: Input: $name = "python", $typed = "perl" Output: false Example #4: Input: $name = "coff...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #312 ("Minimum Time" and "Balls and Boxes")

Image
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 The Weekly Challenge for the week of 2025-03-10 through 2025-03-16 is #312. The tasks for challenge #312 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 312-1: Minimum Time Submitted by: Mohammad Sajid Anwar You are given a typewriter with lowercase english letters a to z arranged in a circle. Typing a character takes 1 second. Moving the pointer one character clockwise or anti-clockwise also takes 1 second. The pointer initially points to a. Write a script to return minimum time it takes to print a given string. Example #1: Input: $str = "abc" Output: 5 The pointer is at 'a' initially. 1 sec - type the letter 'a' 1 sec - move pointer clockwise to 'b' 1 sec - type the letter 'b' 1 sec - move pointer clockwise to 'c...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #311 ("Upper Lower" and "Group Digit Sum")

Image
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 The Weekly Challenge for the week of 2025-03-03 through 2025-03-09 is #311. The tasks for challenge #311 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 311-1: Upper Lower Submitted by: Mohammad Sajid Anwar You are given a string consisting of english letters only. Write a script to convert lower case to upper and upper case to lower in the given string. Example #1: Input: $str = "pERl" Output: "PerL" Example #2: Input: $str = "rakU" Output: "RAKu" Example #3: Input: $str = "PyThOn" Output: "pYtHoN" This can be easily done with a one-liner using Perl's "transpose" operator, "tr", aka "y": y/[a-zA-Z]/[A-Za-z]/,print while <> Robbie Hatley's Pe...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #310 ("Arrays Intersection" and "Sort Odd Even")

Image
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 The Weekly Challenge for the week of 2025-02-24 through 2025-03-02 is #310. The tasks for challenge #310 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 310-1: Arrays Intersection Submitted by: Mohammad Sajid Anwar You are given a list of arrays of integers. Write a script to return the common elements in all the arrays. Example #1: Input: $list = ( [1, 2, 3, 4], [4, 5, 6, 1], [4, 2, 1, 3] ) Output: (1, 4) Example #2: Input: $list = ( [1, 0, 2, 3], [2, 4, 5] ) Output: (2) Example #3: Input: $list = ( [1, 2, 3], [4, 5], [6] ) Output: () I'll start by writing a subroutine that determines whether a given integer is in a given array of integers. Then I'll write a sub that determines whether a given integer is in all of the given arrays. Finally, I'll w...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #309 ("Min Gap" and "Min Diff")

Image
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 The Weekly Challenge for the week of 2025-02-17 through 2025-02-23 is #309. The tasks for challenge #309 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 309-1: Min Gap Submitted by: Mohammad Sajid Anwar You are given an array of integers, @ints, increasing order. Write a script to return the element before which you find the smallest gap. Example #1: Input: @ints = (2, 8, 10, 11, 15) Output: 11 8 - 2 => 6 10 - 8 => 2 11 - 10 => 1 15 - 11 => 4 11 is where we found the min gap. Example #2: Input: @ints = (1, 5, 6, 7, 14) Output: 6 5 - 1 => 4 6 - 5 => 1 7 - 6 => 1 14 - 7 => 7 6 and 7 where we found the min gap, so we pick the first instance. Example #3: Input: @ints = (8, 20, 25, 28) Output: 28 8 - 20 => 14 25 - 20 => 5 28 - 25 ...