# Blog

• PWC 110 › Phone Number Validation
Personal note: I’ve had to take a break from participating in the PWC, but I’m back for this week, at least. Hopefully I’ll be able to contribute more again. The first task this week is a sort of simple phone number validation, based on provided templates. Numbers must match the following, where n is any …
• PWC 110 › Transpose CSV File
The second task this week is simple: given a (simplified) comma-separated-value (CSV) file, transpose its rows and columns. For example: The challenge task does not actually refer to the input as CSV, so I’m using that term loosely, with simplified parsing to match the input specification. If more compliant parsing is needed, one could use …
• PWC 056 › Path Sum
Task #2 this week is a simple tree traversal: You are given a binary tree and a sum, write a script to find if the tree has a path such that adding up all the values along the path equals the given sum. Only complete paths (from root to leaf node) may be considered for …
• PWC 056 › Diff-K
Task #1 this week is to implement the Diff-K algorithm, as explained by Mohammad: You are given an array @N of positive integers (sorted) and another non negative integer k. Write a script to find if there exists 2 indices i and j such that A[i] – A[j] = k and i != j. This one is pretty easy. We can boil down the solution …
• PWC 054 › Collatz Conjecture
Task #2 this week was one of my devising: It is thought that the following sequence will always reach 1: For example, if we start at 23, we get the following sequence: 23 → 70 → 35 → 106 → 53 → 160 → 80 → 40 → 20 → 10 → 5 → 16 → …
• PWC 054 › kth Permutation
Task #1 this week is as follows: Write a script to accept two integers n (>=1) and k (>=1). It should print the kth permutation of n integers. For more information, please follow the wiki page. For example, for n=3 and k=4, the possible permutation sequences are 123, 132, 213, 231, 312, 321. The script should print the 4th permutation sequence, 231. This is fairly straightforward. There are …
• PWC 053 › Vowel Strings
Task #2 this week has us construct “vowel strings,” as described by Mohammad: Write a script to accept an integer 1 ≤ N ≤ 5 that would print all possible strings of size N formed by using only vowels (a, e, i, o, u). The string should follow the following rules: ‘a’ can only be followed by ‘e’ and ‘i’. ‘e’ can only be …
• PWC 053 › Matrix Rotation
Task #1 this week is as follows: Write a script to rotate the following matrix by given 90/180/270 degrees clockwise. At first glance, I thought this was a simple matrix transpose, which is what you get when you swap the rows and columns of a matrix. The transposition (T) of the example matrix would give us:
• PWC 052 › Lucky Winner
Task #2 this week can be as simple or as difficult as you make it. Mohammad’s description: Suppose there are following coins arranged on a table in a line in random order. £1, 50p, 1p, 10p, 5p, 20p, £2, 2p Suppose you are playing against the computer. Player can only pick one coin at a …
• PWC 052 › Stepping Numbers
Task #1 this week is straightforward. Here’s what Mohammad had to say about it: Write a script to accept two numbers between 100 and 999. It should then print all Stepping Numbers between them. A number is called a stepping number if the adjacent digits have a difference of 1. For example, 456 is a stepping number …
• PWC 051 › 3Sum and Colourful Numbers
Personal note: It’s been an extremely challenging couple of weeks for me, due to a family emergency. As such I’m combining my solutions into a single, shorter blog post this week. If you also follow my review posts on the perlweeklychallenge.org site, you’ll note they are quite late as well. I’m sorry about that! Hopefully …
• PWC 050 › Noble Integers
Task #2 this week is described as follows: You are given a list, @L, of three or more random integers between 1 and 50. A Noble Integer is an integer N in @L, such that there are exactly N integers greater than N in @L. Output any Noble Integer found in @L, or an empty list if none were found. An interesting question is whether …
• PWC 050 › Merge Intervals
Task #1 this week asks us to merge integer ranges. Here is the task description: Write a script to merge the given intervals where ever possible. [2,7], [3,9], [10,12], [15,19], [18,22] The script should merge [2, 7] and [3, 9] together to return [2, 9]. Similarly it should also merge [15, 19] and [18, 22] together to return [15, 22]. The final result should be …
• PWC 049 › LRU Cache
Task #2 this week has us build an LRU cache, which is a cache of fixed capacity, whereby when it is full but a new item needs to be added, the item that was least-recently accessed is evicted from the cache. For example, if a cache of capacity = 3 contains [a b c] (where …
• PWC 049 › Smallest multiple containing only 1 and 0
Task #1 this week is as follows: Write a script to accept a positive number as command line argument and print the smallest multiple of the given number consists of digits 0 and 1. Brute Force It’s easy enough to brute force this problem, by checking every multiple in order: \$_ now contains the answer. Unfortunately, some numbers—particularly …
• PWC 048 › Survivor (Josepheus problem)
Task #1 this week is described as follows: There are 50 people standing in a circle in position 1 to 50. The person standing at position 1 has a sword. He kills the next person i.e. standing at position 2 and pass on the sword to the immediate next i.e. person standing at position 3. …
• PWC 048 › Palindrome Dates (mm/dd/yyyy)
Task #2 this week asks us to (and I quote): print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001. It’s pretty easy to avoid using any sort of date library with a couple of key observations about the problem domain. First, we’ll split …
• PWC 047 › Roman Calculator
The first challenge this week tasks us with evaluating arithmetic operations using Roman numerals. The description seems to indicate that only one operation will be given (for example, II + IV), but I have elected to support arbitrary arithmetic expressions. First, I needed a way to convert to and from Roman and Arabic numerals. I …
• PWC 047 › Gapful Numbers
Task 2 this week has us print the first 20 “gapful numbers,” as described by OEIS sequence A108343. Gapful numbers are numbers greater than 99 that are evenly divisible by their first and last digits combined. For example, 132 is a gapful number because 132 ÷ 12 = 11. This is certainly the easier of …
• PWC 046 › Cryptic Message
Challenge #1 this week is the following: The communication system of an office is broken and message received are not completely reliable. To send message Hello, it ended up sending these following:
• PWC 046 › Is the Room Open? (500 Doors)
The second of the challenges this week poses the following question (paraphrased): Suppose we have 500 doors, and 500 employees. The first employee opens all the doors. The second employee closes every 2nd door (doors 2, 4, 6, … 500). The third employee closes every third door if it is open, or opens it if …
• Quine
Challenge #2 this week asks for “a script that dumps its own source code”. This is almost a quine, although Mohammad did not name it as such. Specifically, we are given the constraint that perl ch-2.pl | diff – ch-2.pl must return nothing. What’s a Quine? A quine, otherwise known as a self-replicating program, is …
• Square Secret Code
Task #1 this week is a simple cipher, described as follows: The square secret code mechanism first removes any space from the original message. Then it lays down the message in a row of 8 columns. The coded message is then obtained by reading down the columns going left to right. Given “The quick brown …
• Make it \$200
For challenge #2 this week, the task is to start with \$1, and by either adding \$1 or doubling the amount, reach \$200 in the smallest possible number of steps. “Greedy never works” In a 75-minute lecture some decades ago, my Advanced Algorithms professor said, over and over, “greedy never works,” while all the while …
Challenge #1 this week is as follows: You are given a string “123456789”. Write a script that would insert ”+” or ”-” in between digits so that when you evaluate, the result should be 100. I am going to add the additional constraint that we want all possible solutions, because that is a much stronger statement, and not that difficult to …
• Self-descriptive Numbers
Challenge #2 this week (43) is to generate Self-descriptive Numbers in arbitrary bases. A self-descriptive number, as described by Wikipedia, is an integer m that in a given base b is b digits long in which each digit d at position n (the most significant digit being at position 0 and the least significant at position b – 1) counts how many instances of digit n are …
• Olympic Rings
Challenge #1 this week (43) is a number puzzle. In short, the task is to fill in the numbers 1, 2, 3, 4, and 6 into the spaces within the intersecting Olympic rings, so that the numbers in each ring sum to 11. Some of the spaces are already filled in (given). Here is the …
• Balanced Parentheses
The second challenge this week is another old chestnut: Write a script to generate a string with random number of ( and ) brackets. Then make the script validate the string if it has balanced brackets. The question has two parts:
• Octal Representation
Challenge 1 this week is an easy one: Write a script to print decimal number 0 to 50 in [the] Octal Number System. Perl and Raku We can solve this with the following polyglot (runs in both languages at once): Still, in Raku, we can do the following:
• Leonardo Numbers
Happy new year! We are on Week 41, and this is Challenge #2. The Leonardo Numbers (A001595) are a simple recursively defined sequence: The sequence starts: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, … You’ll note this sequence is very similar …
• Attractive Numbers
This week, Mohammad asks us to output a list of all Attractive Numbers between 1 and 50. Attractive numbers, as described by the Online Encyclopedia of Integer Sequences (OEIS) are: Numbers with a prime number of prime divisors (counted with multiplicity) OEIS Sequence A063989 The first numbers are 4, 6, 8, 9, 10, 12, 14, …
• Zip6
The zip6 function has long been a staple of the List::MoreUtils CPAN module. The Week 40 challenge #1 describes it very well. But even more succinctly: zip6 takes an array of arrays (AoA) and returns another AoA of all the 1st elements, then the 2nd, and so on. It’s not a difficult algorithm by any …
• Reverse Polish Notation
This is my first blog post regarding the Perl Weekly Challenge tirelessly maintained by Mohammad Anwar. Although I’ve been doing the challenge for a while now, I haven’t maintained my blog for years. Let’s change that! This week’s challenge #2 from the Perl Weekly Challenge is a computer science classic: create a Reverse Polish Notation …