Robbie Hatley's Solutions To The Weekly Challenge #234

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-09-10 through 2023-09-16) is weekly challenge #234.

Task 1 is as follows:

Task 1: Common Characters
Submitted by: Mohammad S Anwar
You are given an array of words made up of alphabetic characters only.
Write a script to return all alphabetic characters that show up in all
words, including duplicates.

Example 1:
Input: @words = ("java", "javascript", "julia")
Output: ("j", "a")

Example 2
Input: @words = ("bella", "label", "roller")
Output: ("e", "l", "l")

Example 3
Input: @words = ("cool", "lock", "cook")
Output: ("c", "o")

My solution was to solve this by making these four subroutines:
sub is_alpha ($aref); # Return boolean indicating whether-or-not an array contains only alphabetical strings.
sub unique_letters ($aref); # Return list of unique letters in the strings of array.
sub copies ($letter, $string); # Return number of copies of $letter which exist in string.
sub min_copies ($letter, $aref); # Return minium of numbers of copies of letter in strings of array.
Then follow this algorithm for each array:
1. Make sure array is alphabetic.
2. Make an array "@letters" of the unique letters from the array.
3. Make an array "@output" to hold output.
4. For each $letter of @letters, push min_copies($letter, $aref) copies of $letter to @output.
The script I came up with was this:

Robbie Hatley's Solution to The Weekly Challenge 234-1

Task 2 is as follows:

Task 2: Unequal Triplets
Submitted by: Mohammad S Anwar

You are given an array of positive integers. Write a script
to find the number of triplets (i, j, k) that satisfies
num[i] != num[j], num[j] != num[k] and num[k] != num[i].

Example 1:
Input: @ints = (4, 4, 2, 4, 3)
Ouput: 18
(0, 2, 4) because 4 != 2 != 3 x 6 permutations = 6 solutions
(1, 2, 4) because 4 != 2 != 3 x 6 permutations = 6 solutions
(2, 3, 4) because 2 != 4 != 3 x 6 permutations = 6 solutions
Total = 18 solutions.

Example 2:
Input: @ints = (1, 1, 1, 1, 1)
Ouput: 0
(0 combinations x 0 permutations = 0 solutions)

Example 3:
Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1)
Output: 168
triplets of 1, 4, 7:
3x2×2 = 12 combinations x 6 permutations =  72 solutions
triplets of 1, 4, 10:
3×2×1 = 6  combinations x 6 permutations =  36 solutions
triplets of 4, 7, 10:
2×2×1 = 4  combinations x 6 permutations =  24 solutions
triplets of 1, 7, 10:
3x2x1 = 6  combinations x 6 permutations =  36 solutions
Total = 168 solutions

To solve this problem, I used triple-nested 3-part loops for this. But since it's not specified that i < j < k, I let all three indices travel the entire range, but insisted on $j != $i, $k != $j, $k != $i. Then in the body of the inner-most loop, I wrote:
next if $$aref[$i] == $$aref[$j] || $$aref[$j] == $$aref[$k] || $$aref[$k] == $$aref[$i];
push @unequal_index_triplets, [$i, $j, $k];
The script I ended up with is this:

Robbie Hatley's Solution to The Weekly Challenge 234-2

That's it for 234; see you on 235!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262