Posts

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-08-11 through 2025-08-17 is #334 The tasks for challenge #334 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 334-1: Range Sum Submitted by: Mohammad Sajid Anwar You are given a list integers and pair of indices. Write a script to return the sum of integers between the given indices (inclusive). Example #1: Input: @ints = (-2, 0, 3, -5, 2, -1), $x = 0, $y = 2 Output: 1 Example #2: Input: @ints = (1, -2, 3, -4, 5), $x = 1, $y = 3 Output: -3 Example #3: Input: @ints = (1, 0, 2, -1, 3), $x = 3, $y = 4 Output: 2 Example #4: Input: @ints = (-5, 4, -3, 2, -1, 0), $x = 0, $y = 3 Output: -2 Example #5: Input: @ints = (-1, 0, 2, -3, -2, 1), $x = 0, $y = 2 Output: 1 This is just a matter of using "sum0...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #333 (“Straight Line” and “Duplicate Zeros”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-08-04 through 2025-08-10 is #333 The tasks for challenge #333 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 333-1: Straight Line Submitted by: Mohammad Sajid Anwar You are given a list of co-ordinates. Write a script to find out if the given points make a straight line. Example 1 Input: @list = ([2, 1], [2, 3], [2, 5]) Output: true Example 2 Input: @list = ([1, 4], [3, 4], [10, 4]) Output: true Example 3 Input: @list = ([0, 0], [1, 1], [2, 3]) Output: false Example 4 Input: @list = ([1, 1], [1, 1], [1, 1]) Output: true Example 5 Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]) Output: true I'll consider points A, B, C to be collinear if...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #332 (“Binary Date” and “Odd Letters”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-07-28 through 2025-08-03 is #332 The tasks for challenge #332 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 332-1: Binary Date Submitted by: Mohammad Sajid Anwar You are given a date in the format YYYY-MM-DD. Write a script to convert it into binary date. Example #1: Input: $date = "2025-07-26" Output: "11111101001-111-11010" Example #2: Input: $date = "2000-02-02" Output: "11111010000-10-10" Example #3: Input: $date = "2024-12-31" Output: "11111101000-1100-11111" To solve this, I'll make two subroutines: "dec2bin" which converts positive integers expressed as strings of decimal digits into strings of binary digits, and "d...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #331 (“Last Word” and “Buddy Strings”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-07-21 through 2025-07-27 is #331 The tasks for challenge #331 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 331-1: Last Word Submitted by: Mohammad Sajid Anwar You are given a string. Write a script to find the length of last word in the given string. Example #1: Input: $str = "The Weekly Challenge" Output: 9 Example #2: Input: $str = " Hello World " Output: 5 Example #3: Input: $str = "Let's begin the fun" Output: 3 There are a number of ways of approaching this, including using "split" to obtain a list of words which are in the string. But I'll use a simpler approach: I'll use an m// operator with a (capture group) to isolate the final word...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #330 (“Clear Digits” and “Title Capital”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-07-14 through 2025-07-20 is #330 The tasks for challenge #330 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 330-1: Clear Digits Submitted by: Mohammad Sajid Anwar You are given a string containing only lower case English letters and digits. Write a script to remove all digits by removing the first digit and the closest non-digit character to its left. Example #1: Input: $str = "cab12" Output: "c" Round 1: remove "1" then "b" => "ca2" Round 2: remove "2" then "a" => "c" Example #2: Input: $str = "xy99" Output: "" Round 1: remove "9" then "y" => "x9" Round 2: remove ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #328 (“Replace All ?” and “Good String”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-06-30 through 2025-07-06 is #328 The tasks for challenge #328 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 328-1: Replace all ? Submitted by: Mohammad Sajid Anwar You are given a string containing only lower case English letters and "?". Write a script to replace all "?" in the given string so that the string doesn’t contain consecutive repeating characters. Example 1 Input: $str = "a?z" Output: "abz" There can be many strings, one of them is "abz". The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'. Example 2 Input: $str = "pe?k" Output: "peak" Example 3 Inpu...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #329 (“Counter Integers” and “Nice String”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-07-07 through 2025-07-13 is #329 The tasks for challenge #329 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 329-1: Counter Integers Submitted by: Mohammad Sajid Anwar You are given a string containing only lower case English letters and digits. Write a script to replace every non-digit character with a space and then return all the distinct integers left. Example #1: Input: $str = "the1weekly2challenge2" Output: 1, 2 2 is appeared twice, so we count it one only. Example #2: Input: $str = "go21od1lu5c7k" Output: 21, 1, 5, 7 Example #3: Input: $str = "4p3e2r1l" Output: 4, 3, 2, 1 I think I'll approach this by first using a regular expression in a s/// statement to chan...