Posts

Robbie Hatley's Solutions To The Weekly Challenge #231

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-08-20 through 2023-08-26) is weekly challenge #230. Task 1 is as follows: Task 1: Min Max Submitted by: Mohammad S Anwar You are given an array of distinct integers. Write a script to find all elements which are neither minimum nor maximum. Return -1 if you can’t. Example 1: Input: @ints = (3, 2, 1, 4) Output: (3, 2) (The minimum is 1 and maximum is 4 in the given array. So (3, 2) are the elements which are neither min nor max.) Example 2: Input: @ints = (3, 1) Output: -1 (With only two distinct elements, each element is either min or max.) Example 3: Input: @ints = (2, 1, 3) Output: (2) (The minimum is 1 and maximum is 3 in the given array. So the only element that's neither min nor max is 2.) I suppose I could use CPAN, but I feel like rolling my own tonight. Since the...

Robbie Hatley's Solutions To The Weekly Challenge #230

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-08-13 through 2023-08-19) is weekly challenge #230. Task 1 is as follows: Task 1: Separate Digits Submitted by: Mohammad S Anwar You are given an array of positive integers. Write a script to separate the given array into single digits. Example 1: Input: @ints = (1, 34, 5, 6) Output: (1, 3, 4, 5, 6) Example 2: Input: @ints = (1, 24, 51, 60) Output: (1, 2, 4, 5, 1, 6, 0) In Perl, any integer scalar can be treated like a string, so this task can be done by using "split" to chop the integers into arrays of digits, then dump them into a new array: my @new = map {split //, $_} @old; The script I came up with is this: Robbie Hatley's Solution to The Weekly Challenge 230-1 Task 2 is as follows: Task 2: Count Words Submitted by: Mohammad S Anwar You...

Robbie Hatley's Solutions To The Weekly Challenge #229

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-08-06 through 2023-08-12) is weekly challenge #229. Task 1 is as follows: Task 1: Lexicographic Order Submitted by: Mohammad S Anwar You are given an array of strings. Write a script to delete each string in which the letters do not occur in a lexicographically-sorted order, either forwards or backwards, and return a count of deletions. Example 1: Input: @str = ("abc", "bce", "cae") Output: 1 ("cae" is the only element which is not lexicographically sorted.) Example 2: Input: @str = ("yxz", "cba", "mon") Output: 2 ("yxz" and "mon" are not lexicographically sorted.) I'll take the approach of writing a sub to sort strings forward and backward, then comparing each string to its forw...

Robbie Hatley's Solutions To The Weekly Challenge #228

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-30 through 2023-08-05) is weekly challenge #228. Task 1 is as follows: Task 1: Unique Sum Submitted by: Mohammad S Anwar You are given an array of integers. Write a script to find the sum of unique elements in the given array. Example 1: Input: @int = (2, 1, 3, 2) Output: 4 In the given array we have 2 unique elements (1, 3). Example 2: Input: @int = (1, 1, 1, 1) Output: 0 In the given array no unique element found. Example 3: Input: @int = (2, 1, 3, 4) Output: 10 In the given array every element is unique. To solve this problem, I made a hash of the number of instances of each element in an array, then used "map" to sum-up just those hash keys who's values are exactly 1: Robbie Hatley's Solution to The Weekly Challenge 228-1 Task ...

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