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.