Perl weekly challenge 94
The two challenges this week were a nice introduction to the new year.
Challenge 1 - Group words into groups of anagrams.
This is a nice hash or "arrayref"s question - a Perl staple. For each group we need to generate a key, and put every anagram into this bin.
The simplest key is just to sort the letters into alphabetical order:
join q(), sort split m{}
This means the meat of the method can be written as a one liner.
sub group_anagrams {
my $anagrams = {};
push @{ $anagrams->{join q(),sort split m{}} }, $_ foreach @_;
return $anagrams;
}
Challenge 2 - Flattening Trees & Linked Lists
Again with simple Tree and LinkedList classes this becomes a good
example of simple OO coding.
Leave a comment