Perl Weekly Week 5 (Part 1)

Tags:

So this weeks challenge was all about Anagrams and was very interesting. Because both of the solutions were Anagram based I ended up making a library used by both (and in the case of the second test all 3 of my attempted solutions) and I'm going to cover that first.

Anagram Library

So as this is a simple script I don't plan to release as a module or anything I'm not going full out with a META6.json file just a lib folder in the same folder as the scripts and inside that a file called Anagrams.pm6. (Note Perl6 modules can have iether a .pm6 or a .p6 extension. I think the latter is now recommended... but I'm sometimes slow to catch up.)

Our module starts by declaring it's namespace. As everything in the file is considered part of the module I can just use a unit declaration to state that (if you want to have multiple namespaces in a file you need to use blocks for scoping).

unit package Anagrams;

Of course a module with just that in would be a bit pointless so let's have a simple function is-anagram-of that given two strings will return true if the first is an anagram of the second. Now I know I'm going to be doing this a lot and that are some cases where we can quickly see it's not going to be true. For instance a word is not an anagram of itself so if our two strings are the same return false :

multi sub is-anagram-of( Str \target, Str \word where * eq target ) is export is pure { 
    False; 
}

I'll go into this in a bit more detail as there's a lot here and once you get your head around it it's kind of awesome :

  • multi sub is-anagram-of : This is a multi sub which means there will be more than one code path for it with different arguments.
  • Str \target : I'm defining my variables as a immutable Strings. Immutability is important if you want to easily leverage threading.
  • where * eq target : So this where clause is True if the two strings are the same
  • is export : I want this function to be exported to the callers scope.
  • is pure : For any given pair of strings this function always returns the same result. Can help the VM to speed up the code.
  • { False; } : No need for a return. The last thing evaluated is the return value for the sub.

Another case that's going to turn up a lot is when the two strings are not the same length. Then there's now way they can be anagrams :

multi sub is-anagram-of( Str \target, Str \word where *.codes != target.codes ) is export is pure { 
    False; 
}

Here I use the .codes method to count unicode code points which is generally What you want to do TM.

With those two False cases out of the way I can now test my strings. It's at this point I want to have a small digression about English and theft.

Cafes, Faces and Cáfes

The concept of an Anagram can only have been invented by and English speaker. The English language is much like the English people, a bit light fingered when it comes to other cultures stuff. Historically we were really good at wandering to other peoples countries and going "Oh that looks nice I. It's ours now". And we do it to this day with other languages, English speakers see nothing wrong with appropriating words from other languages. (Except Americanisms, those are just wrong.)

But... technically we don't really use accents on letters. We've just got the 26 and get all confused about funny squiggles on top of letters or dangling off the bottom. And don't get me started on ß...

Anyway when I was testing my anagram code I tried looking for the anagram of cafe and got face. And then I tried looking for the anagram of face and got... nothing. Because cafe is actually spelt cáfe. Please note I can't event type á easily. As a native English speaker though I don't care about the accent, just the letters and I'd quite like my anagram checker to be the same, also it would be good if it wasn't case sensitive.

This first part is simple enough lc will give us the string in lowercase. But stripping of accents and other marks that's going to be a pain isn't it?

Itroduction samemark. Which allows you to match the accent information between two strings. And if the second string is shorter than the first then the last character is used for all matching. Which means :

samemark( lc( "Cáfe" ), "a" ) eq "cafe"

That's great and I've got a little function to do that (I call it normal).

sub normal ( Str \word ) is pure {
    samemark( lc( word ), "a" )
}

This function is only used internally so it's not exported.

Back to Anagrams

Ok so to test if a string is an anagram of another string if I take the string, split it into letters and sort the list and then compare the sorted lists they should be the same. As I'm doing that twice I'll make a function to do it :

sub order-string ( Str \word ) is export is pure {
    normal( word ).comb.sort( { $^a cmp $^b } ).join;
}

Note that we're using our normalised string. I'm also joining it again but it would be trivial to return a list and compare that... I just didn't think of it.

With that in place we've got our final is-anagram-of sub :

multi sub is-anagram-of( Str \target, Str \word ) is export is pure {
    normal( target ) ne normal( word ) && order-string( target ) ~~ order-string( word );
}

So you'll see that the first thing we do is compare the normalised strings are not the same. Otherwise cafe would be an anagram of cáfe. This isn't caught by our initial where clause. I thought about doing it there but I figured it's more of an edge case and I didn't want to normalise every string.

So if our two normal strings are different and the ordered versions are the same then they are anagrams. Awesome!

Putting it together

Now I've got this library the script (which weirdly I see I gave a .p6 extension, I'm nothing if not inconsistent).

Firstly some boilerplate :

#!/usr/bin/env perl6

use v6;
use lib 'lib';
use Anagrams;

This loads our Anagrams module and makes sure if we accidentally run this with perl5 it explodes asap.

Then I like to use Perl6's auto generation for usage text and the following gives us a help / h flag.

my %*SUB-MAIN-OPTS = :named-anywhere;

sub USAGE { say $*USAGE }

#| Display Help file
multi sub MAIN ( Bool :h($help) where *.so ) { USAGE(); }

The my %*SUB-MAIN-OPTS = :named-anywhere; allows you to mix and matched named and positional arguments on the command line. $*USAGE is an autogenerated string based on the signatures and comments on the MAIN functions. (#| being a comment tagged to the function below).

For more information why not check out the talk I gave at London Perl Mongers last year?

With that out of the way here's the meat of the script :

subset FilePath of Str where *.IO.f;

#| Find the anagrams for a given word
multi sub MAIN (
    Str $word, #= Word to check for. Case insensitive
    FilePath :$dict = "/etc/dictionaries-common/words" #= Dictionary file to use. Defaults to "/etc/dictionaries-common/words"
) {
    $dict.IO.words.grep( { is-anagram-of( $word, $_ ) } )>>.say;
}

FilePath is a simple subset to ensure that a given string is actually a file, but the main function is no quite simple. $dict.IO.words opens our files and reads string from it splitting on white space (in this case newlines) this is a Sequence that can be iterated over. grep applies our is-anagram-of function with out given word and keeps those that are then we use the hyper operator and .say to print out any that work.

Generally nice and simple. How hard could part 2 be?

Oh ho ho ho ho. Tune in later for Part two of the challenge and be ready to mock my child like ignorance.

Perl Weekly Week 4

Tags:

So this weeks Perl Challenge is coming down to the wire. This is mostly because it's the Easter Holidays and I've been visiting family. Still as I did propose one of the two challenges I probably should do them.

Pi to n digits

The first challenge was to calculate Pi to N digits. I had a plan to go to Wikipedia an look into how to calculate the digits of Pi. Then Laurent Rosenfeld posted about the Bailey–Borwein–Plouffe formula in the Perl6 user mailing list.

So that made life a bit easier. One thing I did want was to ensure I use FatRat's and I thought this would be a good time to create a constuctor for them giving us this:

use v6;

sub infix:<///> ( Int() $nu, Int() $de ) { FatRat.new( $nu, $de ) }

sub bbp-digit ( Int $k ) {
    my $k8 = $k*8;
    (1 /// (16 ** $k)) * ((4 /// ($k8 + 1)) - (2 /// ($k8 + 4)) - (1 /// ($k8 + 5)) - (1 /// ($k8+6)));
}

sub calc-pi( Int $num ) {
    my $p = [+] ( 0..$num ).map( &bbp-digit );
    return $p.Str.substr(0,$num+1);
}

#| Calculate PI to a given number of digits
sub MAIN (
    Int $digits #= Number of digits to calcuate
) {
    say calc-pi( $digits );
}

Word puzzle solver

So this was my idea for a challenge and it comes from my spending too much time playing random word games on my phone while on the train to work. Some of these games entail making words from a limited set of letters and when I get stumped I tend to dive into the /usr/share/dict folder and start grepping. I'd kept meaning to writing a Perl6 script to do it for me and suggested it to Mohammad.

As I'm writing this I've got 25 minutes to hand in my homework so I'll post this quickly and them maybe come back to it and explain it more later :

use v6;

subset FilePath of Str where *.IO.f;

#| Given a dictionary of words to check and a list of available letters
#| Print the words that can be made using just those letters
sub MAIN (
    FilePath $dict, #= Dictionary file to read
    *@letters #= List of letters allowed to be used
) {
    my $check-set = bag( @letters.map( *.lc ) );

    $dict.IO.words.race.grep( { bag( $_.lc.comb ) (<=) $check-set } )>>.say;
}

Using Bags and set operators plus the race command and it will check the 102,000 lines American English dictionary for a list of 6 letters in 3 seconds. That'll do.

Perl Weekly Week 3

Tags:

So I didn't blog last weeks challenge because it was a very busy week for me but this week I've got a bit of time so here's my solutions to this weeks challenges, in Perl6 for now.

Regular Numbers

I'm not a great mathematician, I've kind of got an instinct for a lot of it but formal math learning and me... have always been uneasy partners so I'd not heard of Regular Numbers before.

One nice thing about that article is it gives a nice algorithm which changes the problem to be "implement this algorithm" and that's something I can do. :)

So this break down to the following :

  1. Make a list with 1 element 1
  2. Take the first element from the list call it h.
  3. Add 2h, 3h, 5h to the list (There's a catch here).
  4. Return h
  5. When you want the next element return to 2

Ok that's nice and looking at it a thought came to mind, this seems a great time to use gather and take to make a sequence of Regular Numbers.

Here's iteration one :

my @h = gather {
    my @items = [1];
    loop {
        my $h = @items.shift;
        @items.push( 2*$h, 3*$h, 5*$h );
        take $h;
    }
}
say @h[0..10];

This.... did not work. It's got a few bugs. Firstly the VM doesn't tag it as a lazy Sequence so it runs the loop forever (Perl6 has for loops for iteration, while loops for testing a truth value and loop which can be used as an infinite loop or as a C style loop).

Fixing the first issue is easy add a lazy in front of the gather and we get some results. But they are a bit all over the place.

(1 2 3 5 4 6 10 6 9 15 10)

Ooooh. Of course when we take 2 from the list we append [4,6,10] to [3,5] and then when we take 3 we append [6,9,15] so on. Now at this point I thought of using a SetHash to ensure I had unique values. But then I'd have to worry about sorting the keys each time. Easier instead to just sort the list and remove unique values. On top of that this seems a good time to add a MAIN wrapper to find out how many numbers are required.

sub MAIN(Int() $count) {
    my @h = lazy gather {
        my @items = (1);
        loop {
            my $h = @items.shift;
            take $h;
            @items.push(2*$h,3*$h,5*h);
            @items = @items.unique.sort( { $^a <=> $^b } );
        }
    };
    say @h[0..$count-1]
}

That's nice and seems to do the trick. Which means it's time for the next challenge.

Pascal's Triangle

So Pascal's Triangle is pretty and I decided I wanted to get it nicely laid out like so:

                   1
                 1   1
               1   2   1
             1   3   3   1
           1   4   6   4   1
         1   5   10  10  5   1
       1   6   15  20  15  6   1
     1   7   21  35  35  21  7   1
   1   8   28  56  70  56  28  8   1
 1   9   36  84 126 126  84  36  9   1

So this challenge has two parts. Generating the numbers for the triangle and then laying it out. In this case I didn't read the article too much but had a neat idea. For a given row (eg [1,2,1] you can generate the next row by implementing doing the following):

# Here's our row
my @row = [1,2,1];
# Make a copy with a 0 at the start
my @row1 = [0, |@row]; # [0,1,2,1] (| breaks the array down so it doesn't get added as a single object in this case)
# Make a copy with a 0 at the end
my @row2 = [|@row, 0]; # [1,2,1,0]

# For each item in row1 add it to the same item in row2
my @row3 = [ 
    @row1[0] + @row2[0], # 0 + 1 = 1
    @row1[1] + @row2[1], # 1 + 2 = 3
    @row1[2] + @row2[2], # 2 + 1 = 3
    @row1[3] + @row2[3], # 1 + 0 = 1
];

So... that's a thing but it's a bit unwieldy if only there was some kind of way to simplify this. Oh yes, the Zip metaoperator Z. Place a Z between two lists and it will default to applying the , operator to each item in turn to make a new list of lists :

(0, 1, 2, 1) Z (1, 2, 1, 0) => ((0,1), (1,2), (2,1), (1,0))

But you can give Z and inline operator to apply list for instance + giving you :

    (0,1,2,1) Z+ (1,2,1,0) => (1, 3, 3, 1)

Which looks very nice. So if we just wanted our script to out lists of lists that make up Pascal's Triangle we could to this :

# Note the challenge says we need at least 3 lines so we catch that here.
multi sub MAIN( Int() $lines is copy where * > 2 ) {
    # Set up the initial state.
    # Decrement lines.
    my @row = (1);
    my @out = [ [1], ]; # Note we need a trailing comma or the array will be flattened
    $lines--;

    # Did I mention this earlier? Repeat / While is like the Perl5 do / while pair
    repeat { 
        @row = (0, |(@row)) Z+ (|(@row), 0); # Here's the Zip magic
        @out.push( @row.clone ); # We need to clone our row or will get clobbered in the next loop
        $lines--;
    } while ( $lines );

    @out.say;
}

And that works but... well I wanted it pretty. The simple thought would be to join each row with spaces and then join the lot with newlines but just doing that you end up with :

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Which is.... a bit meh.

Of course you could make those lines up then find the length of the last line and left pad all the other by half that amount.

@out = @out.map( { $_.join(" ") } );

my $len = @out[*-1].codes;

@out.map( { ( " " x ( ($len - $_.codes) div 2) ) ~ "$_" } ).join("\n").say;

And that's a bit nicer :

             1
            1 1
           1 2 1
          1 3 3 1
         1 4 6 4 1
       1 5 10 10 5 1
     1 6 15 20 15 6 1
   1 7 21 35 35 21 7 1
  1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

But it falls apart when larger numbers get added. So we need to pad our output based on the length of the biggers number. Here's what I came up with :

sub pad( Str $val, Int $len ) {
    my $diff = $len - $val.codes;
    my $rpad = " " x ( ( $diff div 2 ) + 1 );
    my $lpad = " " x ( ( $diff div 2 ) + ( $diff % 2 ) );
    return "{$lpad}{$val}{$rpad}";
}

Pass in the number to pad and the length of the largest number and it returns the number all laiud out nicely. With that here's my final code :

sub pad( Str $val, Int $len ) {
    my $diff = $len - $val.codes;
    my $rpad = " " x ( ( $diff div 2 ) + 1 );
    my $lpad = " " x ( ( $diff div 2 ) + ( $diff % 2 ) );
    return "{$lpad}{$val}{$rpad}";
}

multi sub MAIN( Int() $lines is copy where * > 2 ) {
    my @row = (1);
    my @out = [ [1], ];
    $lines--;
    my $max = 1;

    repeat {
        @row = (0, |(@row)) Z+ (|(@row), 0);
        @out.push( @row.clone );
        $max = @row.max;
        $lines--;
    } while ( $lines );

    my $len = $max.codes;

    @out = @out.map( -> @list { @list.map( { pad($_.Str,$len) } ).join("") } );

    $len = @out[*-1].codes;

    @out.map( { ( " " x ( ($len - $_.codes) div 2) ) ~ "$_" } ).join("\n").say;
}

multi sub MAIN($) {
    note "Please input a number of lines that must be at least 3";
}

And that seems to do the trick which is fun. (This is the code used to make the triangle at the top).

Perl Weekly Challenge Further thoughts

Tags:

So I've had some thoughts on the Perl Weekly challenge. First up I thought I'd do them in Perl5 as well.

Updating E

The code for this looks very similar to my Perl6 solution (because they aren't that different)

use v5.10;
use strict;

my $s = "Perl Weekly Challenge";
my $c = 0;
$c++ while $s =~ s!e!E!;
say "Updated $s";
say "Number of matches : $c";

All I had to do was tell it to use 5.10 (so I could use say why this is not on by default now I don't know) and change $s ~~ s!e!E! to $s ~= s!e!E! so that was easy enough :)

Fizz Buzz

For FizzBuzz I'll take my Perl6 version and modify it to be Perl5 like. This took a little more work

use v5.10;
use strict;

sub fz { $_[0] % 3 == 0 ? "Fizz" : "" }
sub bz { $_[0] % 5 == 0 ? "Buzz" : "" }
sub fb { my $i = shift; (fz( $i ) . bz( $i ) ) || $i }

say join( "\n", map { fb($_) } (1..20) )

Once again I'm using 5.10 because I like say. Not making operators just simple functions and there's no divisible by operator %% so I have to fall back to %% 3 == 0 but the core idea is the same.

Further FizzBuzzery

While I was thinking about this I realised that fz and bz are basically the same thing. And as has been said to me before repition of code is bad. Now in Perl5 you can make a closure and that's neat but Perl6 gives you some more options.

Lets start with a generic check function (I'm going to drop operators for this next bit and instead play with functional programming).

sub check( Int $n, Str $text, Int $val ) { $val %% $n ?? $text !! "" }

So the fizz function for variable $i is :

check( 3, "Fizz", $i )

Which is lovely but the 3, "Fizz" bit looks a bit magic. It'd be nice to encapsulate that in a closure luckily Perl6 gives us a helpful method on Code blocks to do that. Say hello to assuming.

my &fz = &check.assuming( 3, "Fizz" );
my &bz = &check.assuming( 5, "Buzz" );

Here we define fz as being check assuming you call it with 3 and "Fizz" for the first two arguments. (And bz as 5 and "Buzz").

Of course you can also use named arguments in assuming lets create a checker function that can work with one or more subs that take 1 input and return a String (that might be blank).

sub checker( Int $i, :@refs ) {
    ([~] @refs.map( { $_($i) } ) ) || $i.Str;
}

So we take a list of references and then map over the list with our given number, use the reduction metaoperator to concatenate the results. Using this we can make out fizz-buzz function :

my &fizz-buzz = &checker.assuming( refs => [&fz, &bz] );

Of course since fz and bz are only used here we don't really need to define them before :

my &fizz-buzz = &checker.assuming( refs => [ 
    &check.assuming( 3, "Fizz" ), 
    &check.assuming( 5, "Buzz" ) 
] );

And then we can call our function as before :

sub check( Int $n, Str $text, Int $val ) { $val %% $n ?? $text !! "" }
sub checker( Int $i, :@refs ) {
    ([~] @refs.map( { $_($i) } ) ) || $i.Str;
}
my &fizz-buzz = &checker.assuming( refs => [ &check.assuming( 3, "Fizz" ), &check.assuming( 5, "Buzz" ) ] );
(1..20).map( -> $i { fizz-buzz($i) } ).join("\n").say;

Of course that's a lot of work. But what if later we wanted to play FizzBuzzPingPong where you say "Ping" if it's a prime number and Pong if it's divisible by 2. Well then you can easily make the fizz-buzz-ping-pong function :

my &fizz-buzz-ping-pong = &checker.assuming( refs => [ 
    &check.assuming( 3, "Fizz" ), 
    &check.assuming( 5, "Buzz" ), 
    { $_.is-prime ?? "Ping" !! "" }, 
    &check.assuming( 2, "Pong" ) 
] );

Note that for Ping we need to create a new block but that drops into the array easily enough.

Running this :

(1..30).map( -> $i { fizz-buzz-ping-pong($i) } ).join("\n").say;

Gives us :

1
PingPong
FizzPing
Pong
BuzzPing
FizzPong
Ping
Pong
Fizz
BuzzPong
Ping
FizzPong
Ping
Pong
FizzBuzz
Pong
Ping
FizzPong
Ping
BuzzPong
Fizz
Pong
Ping
FizzPong
Buzz
Pong
Fizz
Pong
Ping
FizzBuzzPong

And... I think that'll do for this challenge.

Addendum

It was pointed out to me that the FizzBuzz challenge was supposed to be one line... Oops.

Here you go.

perl6 -e '(1..20).map( { ( [~] ( $_ %% 3 ?? "Fizz" !! "", $_ %% 5 ?? "Buzz" !! "" ) ) || $_.Str } ).join("\n").say'

Back to brute force. With a bite of meta reduction for the fun of it. I still like the functional stuff though.

Perl Weekly Challenge 1

Tags:

Recently the wonderfully talented Mohammad S Anwar started a new project the Perl Weekly Challenge. As a lover of all (or at least many) things Perl and something of a Perl6 fanatic I figured I would sign up and try to challenge each week using Perl6.

If you've entered the challenge and don't want your result spolied do not read further.

For week one we've got two challenges :

  1. Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ found in the string.
  2. Write one-liner to solve FizzBuzz problem and print number 1-20. However, any number divisible by 3 should be replaced by the word fizz and any divisible by 5 by the word buzz. Numbers divisible by both become fizz buzz.

E challenge

So the obvious thought on this one is to use regular expressions and I initially whipped up a simple one liner :

perl6 -e 'my $s = "Perl Weekly Challenge";"Number of e {($s ~~ m:g/e/).elems.say}";$s ~~ s:g/e/E/;"Updated {$s.say}"'

Here I make use of the fact that if you do a global match you get a list of all the matches and .elems gives you the count. Then I just do a global replace.

Of course I'm having to match twice against the string. Hmmm. After a little thought I realised there's a different way of doing it that's a little neater. This time I'll write it out in a bit more detail.

my $s = "Perl Weekly Challenge";
my $c = 0;
$c++ while $s ~~ s!e!E!;
say "Updated $s";
say "Number of matches : $c";

So here we make use of the fact that without the :g adverb a replace only does one match at a time. Then we simply increment a counter for each time. Note in this case I fall back to one of my standard quoting options ! which I find works quite well when doing web development where / has a tendency to pop up all over the place.

If I can think of a wackier way to do this in Perl6 I will but a simple replace with counter seems the way I'd generally do it.

FizzBuzz

Ah FizzBuzz, if you ever get asked this in an interview try not to answer with "Really?". For those of you who don't know the main thing to remember about FizzBuzz is in the name. Some numbers are divisible by both 3 and 5 and in this case you need to output FizzBuzz. The leading way to fail this test is not take that into account. Again my first attempt to resolve the challenge relied on my patented skills of brute force and ignorance. I sent it in to Mohammad as a one liner but here it is tided up a bit.

sub prefix:<fb> (Int $i) {
    $i %% 15 ?? "FizzBuzz"
             !! $i %% 5 ?? "Buzz"
                        !! $i %% 3 ?? "Fizz"
                                   !! $i
}

(1..20).map( fb * ).join("\n").say

The indentation here is to mainly show how seriously brute force this method is. Firstly I test for the 3 and 5 case (using the %% divisibility operator) then if that fails try 5, then 3 and finally return the number if there's no other matches. Note that I put this into a fb operator that you can use as a prefix to any Integer EG fb 10 (which would return 10). Currently the operator returns a String or an Integer, it would probably be best to make it always return a String.

Anyway once I've got the operator applying it to each number in 1 to 20 is easy enough. Map the operator (using a Whatever Star code block) against each value and then join the results with a newline and output them.

Still the fb operator is a bit... clunky. Can I streamline things?

sub prefix:<fz> (Int $i) { $i %% 3 ?? "Fizz" !! "" }
sub prefix:<bz> (Int $i) { $i %% 5 ?? "Buzz" !! "" }
sub prefix:<fb> (Int $i) { (fz $i ~ bz $i) || $i.Str }

(1..20).map( fb * ).join("\n").say

Continuing with the operator theme (which fankly I probably shouldn't adding cutom operators to your code tends to slow down parse time.... but I like them) I add two new ones fz and bz these return either Fizz, Buzz or a blank string as required. By concatenating the results of fz $i and bz $i I either have a string (Fizz, Buzz or sometimes FizzBuzz) or a blank string. I can then make use of the short circuit ability of the || or operator. If the left hand side evaluates to true (which the blank string won't) then the || will be that side. Otherwise it's $i.Str (now fb always returns a String). Note that I don't need to use return as the last thing evaluated in a block is it's value.

I've got some thoughts on further updates but I quite like this one.