Robbie Hatley's Perl Solutions To The Weekly Challenge #212

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-04-09 through 2023-04-15) is weekly challenge #212.

Task 1 is as follows:

You are given a word having alphabetic characters only, and a list of positive integers of the same length. Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.

This is like a Caesar cipher but with the added kink that every index within the plaintext gets rotated an independent amount. I found it very straightforward to program, just a matter of doing chr(65 + (ord($a)-65+$j)%26) for uppercase and chr(97 + (ord($a)-97+$j)%26) for lowercase:

Robbie Hatley's Perl Solution to The Weekly Challenge 212-1

Task 2 is as follows:

You are given a list of integers and group size greater than zero. Write a script to split the list into equal groups of the given size where integers are in sequential order. If it can’t be done then print -1.

Re-worded, this is asking us to attempt to partition a set S of integers into equal-size partitions of size s such that each part is a set consisting of consecutive integers. Right away we see that this is only possible if the cardinality c of the set is divisible by the part size s, and that if such a partition is possible, the parts will have cardinality c/s. It also follows that at least one part must have as its least element the least element of S, and if we "remove" from S the elements which we use to form each part, then tne next part must have as its least element the least element of the "remaining" elements of S, and likewise until either we have a compliant partition and S is empty, or we run into a dead end and realize that no such partition is possible.

So, I used this "remove elements from set as we form parts, and start each part with least element from remainder" approach to create a solution. And -- surprise, surprise -- I used nested 3-part loops. :-) See here:

Robbie Hatley's Perl Solution to The Weekly Challenge 212-2

That's it for 212; see you on 213!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239