Posts

Showing posts from August, 2022

PWC 180

PWC 180 This week saw two light challenges, which I was able to do in both Perl 5 and Raku despite being busy at work. I also did Challenge 2 in Julia, but not Challenge 1. Challenge 1 is a string-handling task for which Julia is not a typical choice. Challenge 1 (First unique character) The challenge here is to find the first unique character in a given string.  The string is likely to be short (a long string would be unlikely to have unique characters, if naturally generated from a much smaller number of characters). So a two-loop solution should be efficient enough:  one loop to count the occurrences of each character using a hash, and  the second to loop through the characters again, checking the number of occurrences from the hash, and stopping at the first character for which the number of occurrences equals 1.  A twist is that we are asked to return the index position of the character, not the character itself.  I give my Perl 5 subroutine a somewhat retro flavor by just using a

PWC 179

PWC 179 These are somewhat quick and dirty attempts as I have less spare time this week. Challenge 1 (ordinal numbers) Challenge 1 asks for a numeric input to be represented in words (in English) as an ordinal number, e.g., 62 -> sixty-second. The easiest way to do this in Perl 5 is via the Lingua::EN::Numbers package or one of its relatives. The ready-made num2en_ordinal subroutine does what we need. In Raku, it is easiest to use Lingua::EN::Numbers again via Inline::Perl 5. In Julia too, I just run Perl 5 one-liners using Julia's backtick notation ( documented here ). So effectively I have three Perl 5 scripts wrapped slightly differently. Here is my Perl 5 script. Here is my Raku script. Here is my Julia script.   I found out later that Raku has its own Lingua::EN::Numbers , so one doesn't have to call the Perl 5 module. Challenge 2 (Unicode sparkline) Sparklines are inline graphics. We are asked to represent an array of numbers as a sparkline using unicode characters.

PWC 178

PWC 178 I didn't have time to do the full challenge in both Perls + Julia this week, so I have just submitted Challenge 1 in Raku, and Challenge 2 in Perl 5. The choice of language for each challenge is because of the availability of modules that make the respective task easy. I also did a partial solution to Challenge 1 in Julia. Challenge 1 (Quater-imaginary base) This is an offbeat number base proposed by Donald Knuth: base 2i. Read all about it in Wkipedia. We are asked to convert a given base-10 number to base-2i. It is trivial to do this in Raku because of the Base::Any module which converts a number from (almost) any number base to (almost) any number base. (The almost is because it does not handle complex bases with both a real and imaginary part. The wholly imaginary 2i is no problem). This allows easy conversion of any real or complex base-10 number, as I illustrate with a couple of examples from the wikipedia page (accessed 2022-8-17). Here is my Raku solution. Julia h

PWC 177

PWC 177 Challenge 1 (Damm Algorithm) This challenge requires implementing the Damm algorithm which uses an extra check digit to catch errors in entering digits in a numeric code, especially the common transposition errors. In terms of programming, this is a simple matter of setting up a lookup table and then looking it up. I implement two subroutines, closely following the howto in the wikipedia page. "get_check_digit" calculates a check digit for a given number, and "validate" checks if a number is correct based on the extended number including a check digit. In Perl 5 and Raku, I give in to dark primitivist urges and set up the lookup table as a flat list of strings. In Julia, it is easy to implement it as a Matlab-like matrix. This algorithm is useful enough to have its own CPAN module: Algorithm::Damm Here is my Perl 5 script. Here is my Raku script. Here is my Julia script. Challenge 2 (Palindromic Prime Cyclops) This challenge requires looking for the first 2

PWC 176

PWC 176 Challenge 1 (Permuted multiples) We are asked to find the smallest number x such that x , 2 x, 3 x , 4 x , 5 x and 6 x all have the same digits in common (not in the same order obviously) and no other digits. The answer is 142857.    I started with Raku. Looping through the integers, for each multiple, 2x, 3x etc., I used comb to get a list of digits and then sort to get them sorted and then gist to stringify them, and then compared this to the combed, sorted and stringified original number x. This translates easily to both Perl 5 and Julia. In Perl 5 I used a sub to do the equivalent of combing and sorting and then joined the array back into an Int. Here is my Raku script Here is my Perl 5 script Here is my Julia script   Here it is as a Raku one-liner:   raku -e '(1 .. 200_000).grep({$_.comb.sort.gist eq (6*$_).comb.sort.gist}).grep({$_.comb.sort.gist eq (5*$_).comb.sort.gist}).grep({$_.comb.sort.gist eq (4*$_).comb.sort.gist}).grep({$_.comb.sort.gist eq (3*$_).comb.s