Perl Weekly Challenge 076
I did this week's Perl Weekly Challenge in both Perl and Prolog. The Prolog solutions were good practice in shaking the rust off my logic programming but I won't discuss them here, to keep things short. The code for the Prolog solutions for Part 1 and Part 2 are on GitHub.
You are given a number $N. Write a script to find the minimum number of prime numbers required, whose summation gives you $N. For the sake of this task, please assume 1 is not a prime number.
For this solution I used a pre-computed list of the first 1000 prime numbers. Larger pre-computed lists are available and of course computing them directly is always an option too! For the purposes of this challenge it seemed a pre-computed list would be OK.
$ perl perl/ch-1.pl 9
7 + 2 = 9
$ perl perl/ch-1.pl 87
83 + 2 + 2 = 87
Write a script that takes two file names. The first file would contain word search grid as shown below. The second file contains list of words, one word per line. You could even use local dictionary file.
Print out a list of all words seen on the grid, looking both orthogonally and diagonally, backwards as well as forwards.
I opted to hard code the grid and load a small dictionary file. This file was obtained from http://www-personal.umich.edu/~jlawler/wordlist.html and contains 60,000 of the most common english words. Ultimately this yield a lot of awkward looking two and three letter words which, frankly, I do not personally consider all that common. I used the full dictionary but filter the results to only words with 4 or more letters.
The approach is straightforward:
- Create arrays of all diagonals, columns, rows, etc of the grid.
- Search for all dictionary words (forward and reverse) from all the grid arrays.
- Filter out words with less than 4 letters. Many of these are very uncommon.
- Sort and display the found words.
$ perl perl/ch-2.pl
Found the following words: align,alls,ante,arare,aras,aras,argos,ba