From 040123641612ac7bae4baab726413f3eefe135b7 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Mon, 22 Apr 2019 16:38:21 -0300 Subject: Gustavo Chaves's solutions to PWC 005 --- challenge-005/gustavo-chaves/perl5/ch-1.pl | 16 ++++++++++++++++ challenge-005/gustavo-chaves/perl5/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 challenge-005/gustavo-chaves/perl5/ch-1.pl create mode 100755 challenge-005/gustavo-chaves/perl5/ch-2.pl diff --git a/challenge-005/gustavo-chaves/perl5/ch-1.pl b/challenge-005/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..6024ea5656 --- /dev/null +++ b/challenge-005/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +# Write a program which prints out all anagrams for a given word. For more +# information about Anagram, please check this: +# https://en.wikipedia.org/wiki/Anagram + +use 5.026; +use strict; +use autodie; +use warnings; + +my $word = shift or die "usage: $0 WORD [WORDFILE...]\n"; + +my $key = join('', sort split //, lc $word); + +say foreach grep { chomp; $key eq join('', sort split //, lc $_) } <>; diff --git a/challenge-005/gustavo-chaves/perl5/ch-2.pl b/challenge-005/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..cbcc7370cc --- /dev/null +++ b/challenge-005/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Write a program to find the sequence of characters that has the most anagrams. + +use 5.026; +use strict; +use autodie; +use warnings; +use List::Util 'max'; + +my %anagrams; + +# Read list of words and classify them by anagrams +while (<>) { + chomp; + my $key = join('', sort split //, lc $_); + push @{$anagrams{$key}}, $_; +} + +# Find out the cardinality of the biggest anagram set +my $max = max map {scalar @$_} values %anagrams; + +# Print all sequences of letters with most anagrams +while (my ($key, $words) = each %anagrams) { + say $key if @$words == $max; +} -- cgit