aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-22 20:48:52 +0100
committerGitHub <noreply@github.com>2019-04-22 20:48:52 +0100
commite06416290b265e8a8f1fac20264b757f0f2a020c (patch)
tree191fef96bdde19ea1a7f391a5d68abe5ba9b9b24
parent7932b449f41098ce880a55869afa8e13bdd14e31 (diff)
parent040123641612ac7bae4baab726413f3eefe135b7 (diff)
downloadperlweeklychallenge-club-e06416290b265e8a8f1fac20264b757f0f2a020c.tar.gz
perlweeklychallenge-club-e06416290b265e8a8f1fac20264b757f0f2a020c.tar.bz2
perlweeklychallenge-club-e06416290b265e8a8f1fac20264b757f0f2a020c.zip
Merge pull request #87 from gnustavo/gustavo-chaves-005
Gustavo Chaves's solutions to PWC 005
-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;
+}