aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-005/gustavo-chaves/perl5/ch-1.pl16
-rwxr-xr-xchallenge-005/gustavo-chaves/perl5/ch-2.pl26
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;
+}