Posts

Showing posts from August, 2023

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