diff options
| author | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-22 16:38:21 -0300 |
|---|---|---|
| committer | Gustavo L. de M. Chaves <gustavo@cpqd.com.br> | 2019-04-22 16:38:21 -0300 |
| commit | 040123641612ac7bae4baab726413f3eefe135b7 (patch) | |
| tree | 191fef96bdde19ea1a7f391a5d68abe5ba9b9b24 | |
| parent | 7932b449f41098ce880a55869afa8e13bdd14e31 (diff) | |
| download | perlweeklychallenge-club-040123641612ac7bae4baab726413f3eefe135b7.tar.gz perlweeklychallenge-club-040123641612ac7bae4baab726413f3eefe135b7.tar.bz2 perlweeklychallenge-club-040123641612ac7bae4baab726413f3eefe135b7.zip | |
Gustavo Chaves's solutions to PWC 005
| -rwxr-xr-x | challenge-005/gustavo-chaves/perl5/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-005/gustavo-chaves/perl5/ch-2.pl | 26 |
2 files changed, 42 insertions, 0 deletions
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; +} |
