Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #305 ("Binary Prefix" and "Alien Dictionary")
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:
The Weekly Challenge for the week of 2024-01-20 through 2025-01-26 is #305.
The tasks for challenge #305 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 305-1: "Binary Prefix" Submitted by: Mohammad Sajid Anwar You are given a binary array. Write a script to return an array of booleans where the partial binary number up to that point is prime. Example #1: Input: @binary = (1, 0, 1) Output: (false, true, true) Sub-arrays (base-10): (1): 1 - not prime (1, 0): 2 - prime (1, 0, 1): 5 - prime Example #2: Input: @binary = (1, 1, 0) Output: (false, true, false) Sub-arrays (base-10): (1): 1 - not prime (1, 1): 3 - prime (1, 1, 0): 6 - not prime Example #3: Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1) Output: (false, true, true, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true)
For each binary prefix, I'll apply eval to "0b" tacked to the left of the join of the slice of the prefix in order to convert it to decimal; then I'll feed each such decimal number to an "is_prime" function to determine its primality.
Robbie Hatley's Perl Solution to The Weekly Challenge 305-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 305-2: "Alien Dictionary" Submitted by: Mohammad Sajid Anwar You are given a list of words and alien dictionary character order. Write a script to sort lexicographically the given list of words based on the alien dictionary characters. Example #1: Input: @words = ("perl", "python", "raku") @alien = qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/ Output: ("raku", "python", "perl") Example #2: Input: @words = ("the", "weekly", "challenge") @alien = qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/ Output: ("challenge", "the", "weekly")
To solve this problem, I'll first write a "compare" subroutine which compares two words according to a given alphabet, then I'll write a "alien_dictionary" subroutine which sorts a given list of words by feeding them (along with a given alphabet) to my "compare" subroutine.
Robbie Hatley's Perl Solution to The Weekly Challenge 305-2
That's it for challenge 305; see you on challenge 306!
Comments
Post a Comment