The Weekly Challenge

The Weekly Challenge, run by Mohammad Sajid Manwar (https://theweeklychallenge.org/), offers a fun set of programming puzzles. I work through them in Perl. Some are pretty challenging, others are nice and simple.

Below is a small selection of solutions to problems I quickly wrapped my head around and that have short, straightforward implementations. Hopefully, they'll be useful - or at least interesting - on your Perl journey.

The table below shows the challenge number (with a link), a short description of the challenge, and the most important keywords referring to the functions and modules I used to solve it.
Challenge Description Keywords
363-1 Write a script that parses a self-referential string and determines whether its claims about itself are true. The string will make statements about its own composition, specifically the number of vowels and consonants it contains. split, map, word_to_number, extract data, ternary operator
362-2 You are given an array of integers. Write a script to return them in alphabetical order, in any language of your choosing. Default language is English. Schwartzian Transform, Lingua::Any::Numbers, sort, cmp
362-1 You are given a string containing lowercase letters. Write a script to transform the string based on the index position of each character (starting from 0). For each character at position i, repeat it i + 1 time for, map, join, substr
360-2 You are give a sentence. Write a script to order words in the given sentence alphabetically but keeps the words themselves unchanged. join, grep, sort, cmp, split
360-1 You are given a string and a width. Write a script to return the string that centers the text within that width using asterisks * as padding. String::Pad qw(pad)
359-2 You are given a word containing only alphabets, Write a function that repeatedly removes adjacent duplicate characters from a string until no adjacent duplicates remain and return the final word. split, substr
359-1 Write a function that calculates the additive persistence of a positive integer and also return the digital root. split
358-2 Encrypt a string using a Caesar cipher shift by rotating letters through the alphabet. chr, ord, join, split,
358-1 Find the maximum alphanumeric value in an array by treating numeric strings as numbers and others by length. regex
353-1 return the maximum number of words that appear in a single sentence. List::Util qw(max), map, scalar, split
348-1 Check if a string of even length can be split into two halves with equal non-zero vowel counts. substr, transliteration, shift
340-1 Repeatedly remove adjacent duplicate letters from a string until no more duplicates remain. do-while, regex
320-1 Return the larger count between positive and negative integers in an array, ignoring zeros. foreach, ternary operator
319-1 Count words in an array that either start with or end with a vowel. grep, regex
317-1 Return true if the first letters of words in an array form a given acronym. join, map, eq
310-1 Convert uppercase letters to lowercase and lowercase letters to uppercase in a string. tr operator (transliteration)
308-1 Count common strings between two arrays. nested loops
301-1 Arrange positive integers to form the largest possible concatenated number. regex, join, sort, cmp
289-1 Find the third distinct maximum number in an array, or return the maximum if fewer than three distinct values exist. hash, sort, scalar
284-1 Find the lucky integer in an array where the frequency equals its value, returning the largest if multiple. Statistics::Frequency, grep, sort, ternary operator
283-1 Find the unique element in an integer array where all other elements appear more than once. grep, hash
282-2 Count the number of times a user must change the key while typing a string, ignoring shifts and caps locks. transliteration (squeeze option)
280-1 Print the first letter in a string that appears twice. hash, split
279-2 Split a string into two parts with equal vowel counts and indicate if possible. transliteration, ternary operator
278-1 Reassemble a shuffled string where each word contains a number for original position. join, map, regex, sort, substr, split
277-1 Count words that appear exactly once in both string arrays. List::Compare, grep, scalar
276-1 Count the number of pairs in an array that form a complete day (sum is a multiple of 24 hours). Math::Combinatorics
275-2 Replace each digit in an alphanumeric string with the value of the previous letter plus the digit. split, chr, ord
270-2 Find all substrings of a string that start and end with a vowel. string processing, substrings, vowels, regex
270-1 Calculate the factorial of a number using recursion or iteration. factorial, recursion, iteration, mathematical computation
268-2 Number game List::MoreUtils qw(natatime), reverse, join, push
268-1 Magic Number nested loops
267-2 Line Count split, ord
267-1 Find the sign of product of all integers in the given array. grep, modulo
265-2 Completing Word Statistics::Frequency, scalar, sort, join, regex, nested loops
265-1 Write a script to find an integer in the given array that appeared 33% or more Statistics::Frequency, hash, sort
264-2 Merge two arrays into a new array that satisfy specific conditions substr, split
264-1 Write a script to return the greatest English letter in the given string. index, push, sort
263-2 Merge items of 2D array. substr, hash, sort
263-1 Return the list of indices in the sorted array where the element is same as the given target element. join, grep, sort
262-2 Count or identify pairs of elements in an array that satisfy specific conditions on their values and/or positions. nested for loop
262-1 Return the maximum number of either positive or negative integers in the given array scalar, grep, ternary operator
261-1 Evaluate the absolute difference between element and digit sum of the given array. split, join, abs
260-1 Determine if the number of occurrences of each array value is unique. hash
258-1 Write a script to find out how many integers have even number of digits. scalar, grep, regex
255-1 Compare two strings and find the additional character regex, split
239-1 Write a script to find out if the word created by concatenating the array elements is the same. array comparison, join