Posts

Showing posts from July, 2023

Robbie Hatley's Solutions To The Weekly Challenge #227

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-07-23 through 2023-07-29) is weekly challenge #227. Task 1 is as follows: Task 1: Friday 13th Submitted by: Peter Campbell Smith You are given a year number in the range 1753 to 9999. Write a script to find out how many dates in the year are Friday 13th, assuming that the current Gregorian calendar applies. Example: Input: $year = 2023 Output: 2 Since there are only 2 Friday 13th in the given year 2023, i.e. 13th Jan and 13th Oct. Let's use the number 0-6 to stand for days-of-week from a Sunday through the next Saturday. Because we're only interested in years 1753+, lets use 13 December 1752 as "epoch". That was a Wednesday, so our starting offset is 3. Then make an array to hold elements indexed from 1753 through 9999 (representing the years), with element

Robbie Hatley's Solutions To The Weekly Challenge #226

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-07-16 through 2023-07-22) is weekly challenge #226. Task 1 is as follows: Task 1: Shuffle String Submitted by: Mohammad S Anwar You are given a string and an array of indices of same length as string. Write a script to return the string after re-arranging the indices in the correct order. Example 1: Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1) Output: 'challenge' Example 2: Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6) Output: 'perlraku' This problem is the exact opposite of a Perl "slice", in that it asks not "What would this string look like if rearranged in this order?", but rather, "What original string would look like THIS if rearranged in THIS order?", which is a different questio

Robbie Hatley's Solutions To The Weekly Challenge #225

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-07-09 through 2023-07-15) is weekly challenge #225. Task 1 is as follows: Task 1: Max Words Submitted by: Mohammad S Anwar Given a list of sentences, write a script to find the maximum word count of the sentences of the list. Example 1: Input: @list = ( "Perl and Raku belong to the same family.", "I love Perl.", "The Perl and Raku Conference." ); Output: 8 Example 2: Input: @list = ( "The Weekly Challenge.", "Python is the most popular guest language.", "Team PWC has over 300 members.", ); Output: 7 This is just a matter of using split to parse the sentences of each list to words, then counting the words, then using List::Util 'max' to find the maximum wo

Robbie Hatley's Solutions To The Weekly Challenge #224

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-07-02 through 2023-07-08) is weekly challenge #224. Task 1 is as follows: Task 1: Special Notes Submitted by: Mohammad S Anwar Given two strings, $source and $target, write a script to determine if using the characters (only once) from $source, $target can be created. Example 1: Input: $source = "abc", $target = "xyz" Output: false Example 2: Input: $source = "scriptinglanguage", $target = "perl" Output: true Example 3: Input: $source = "aabbcc", $target = "abc" Output: true This problem basically asks "can poison pen letter 'target' be made from source 'source'?". I think I'll use the following algorithm: sub ppl ($source, $target) { # ppl = "Poison Pen Letter" for e

Robbie Hatley's Solutions To The Weekly Challenge #223

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-06-25 through 2023-07-01) is weekly challenge #223. Task 1 is as follows: "You are given a positive integer, $n. Write a script to find the total count of primes less than or equal to the given integer." This is just a matter of counting all the odd numbers up to $n which are prime (plus 1 to account for 2): Robbie Hatley's Solution to The Weekly Challenge 223-1 Task 2 is as follows: You are given an array representing box coins, @box. Write a script to collect the maximum coins until you took out all boxes. If we pick box[i] then we collect the coins $box[i-1] * $box[i] * $box[i+1]. If $box[i+1] or $box[i-1] is out of bound then treat it as 1 coin. Example 1: Input: @box = (3, 1, 5, 8) Output: 167 Step 1: pick box [i=1] and collected coins 3 * 1 * 5 =&g