Robbie Hatley's Solutions To The Weekly Challenge #233
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:
This week (2023-09-03 through 2023-09-09) is weekly challenge #233.
Task 1 is as follows:
Task 1: Similar Words Submitted by: Mohammad S Anwar You are given an array of words made up of alphabetical characters only. Write a script to find the number of pairs of "similar" words. Two words are "similar" if they consist of the same characters. Example 1: Input: @words = ("aba", "aabb", "abcd", "bac", "aabc") Output: 2 Pair 1: similar words ("aba", "aabb") Pair 2: similar words ("bac", "aabc") Example 2: Input: @words = ("aabb", "ab", "ba") Output: 3 Pair 1: similar words ("aabb", "ab") Pair 2: similar words ("aabb", "ba") Pair 3: similar words ("ab", "ba") Example 3: Input: @words = ("nba", "cba", "dba") Output: 0
To solve this problem, we need only obtain the "signature" (sorted sequence of unique characters) of each "alphabetical" (/^[a-zA-Z]+$/) string, and determine which string pairs have the same signatures. "split", "join", "sort", and "uniq" from List::Util will all be helpful here:
Robbie Hatley's Solution to The Weekly Challenge 233-1
Task 2 is as follows:
Task 2: Frequency Sort Submitted by: Mohammad S Anwar You are given an array of integers. Write a script to sort the given array in increasing order based on the frequency of the values. If multiple values have the same frequency then sort them in decreasing order. Example 1: Input: @ints = (1,1,2,2,2,3) Ouput: (3,1,1,2,2,2) '3' has a frequency of 1 '1' has a frequency of 2 '2' has a frequency of 3 Example 2: Input: @ints = (2,3,1,3,2) Ouput: (1,3,3,2,2) '2' and '3' both have a frequency of 2, so they are sorted in decreasing order. Example 3: Input: @ints = (-1,1,-6,4,5,-6,1,4,1) Ouput: (5,-1,4,4,-6,-6,1,1,1)
To solve this problem, I'll first hash all the integers by frequency, then reverse sort each frequency bin by value:
Robbie Hatley's Solution to The Weekly Challenge 233-2
That's it for 233; see you on 234!
Comments
Post a Comment