aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorbagheera-sands <git@sandsscouts.org.uk>2019-04-22 22:33:48 +0100
committerbagheera-sands <git@sandsscouts.org.uk>2019-04-22 22:33:48 +0100
commit5ee061e57de12fb9071bb2fcd3a26ea7ce2cecf6 (patch)
treea41dbc8ac591126520b152b19ee7186a6503875b /challenge-005
parent7d54035074ef1c522c3d0460ba828ef69f8efd7e (diff)
parentef12b6bb075c198fb775acf5d2dc04e25cf9c283 (diff)
downloadperlweeklychallenge-club-5ee061e57de12fb9071bb2fcd3a26ea7ce2cecf6.tar.gz
perlweeklychallenge-club-5ee061e57de12fb9071bb2fcd3a26ea7ce2cecf6.tar.bz2
perlweeklychallenge-club-5ee061e57de12fb9071bb2fcd3a26ea7ce2cecf6.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-005')
-rw-r--r--challenge-005/fjwhittle/blog.txt1
-rw-r--r--challenge-005/fjwhittle/perl6/ch-1.p617
-rw-r--r--challenge-005/fjwhittle/perl6/ch-2.p619
-rwxr-xr-xchallenge-005/gustavo-chaves/perl5/ch-1.pl16
-rwxr-xr-xchallenge-005/gustavo-chaves/perl5/ch-2.pl26
-rw-r--r--challenge-005/joelle-maslak/perl6/ch-1-readme2
-rw-r--r--challenge-005/john-barrett/perl5/README.md8
-rw-r--r--challenge-005/rob4t/perl6/ch-1.p616
-rw-r--r--challenge-005/rob4t/perl6/ch-2.p611
9 files changed, 106 insertions, 10 deletions
diff --git a/challenge-005/fjwhittle/blog.txt b/challenge-005/fjwhittle/blog.txt
new file mode 100644
index 0000000000..afa4b8a4ba
--- /dev/null
+++ b/challenge-005/fjwhittle/blog.txt
@@ -0,0 +1 @@
+https://rage.powered.ninja/2019/04/22/anagramming-max.html
diff --git a/challenge-005/fjwhittle/perl6/ch-1.p6 b/challenge-005/fjwhittle/perl6/ch-1.p6
new file mode 100644
index 0000000000..ffee42f289
--- /dev/null
+++ b/challenge-005/fjwhittle/perl6/ch-1.p6
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+#| Find anagrams of a word in given file
+unit sub MAIN(
+ Str $file #= file containing list of words
+ where { given .IO { .r && ( .l || .f) or die "Cannot read from $_" } },
+ $word #= word to find anagrams of
+);
+
+my $word-bag := $word.lc.comb(/ \w /).Bag;
+
+my @words = $file.IO.lines.unique.hyper.grep(*.chars > 2)
+ .map: { .lc.comb(/ \w /).Bag => $_ };
+
+@words.race.grep(*.key === $word-bag)».value.unique(with => *.lc eq *.lc).join(', ').put;
diff --git a/challenge-005/fjwhittle/perl6/ch-2.p6 b/challenge-005/fjwhittle/perl6/ch-2.p6
new file mode 100644
index 0000000000..75cdfa6a55
--- /dev/null
+++ b/challenge-005/fjwhittle/perl6/ch-2.p6
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+#| Find the sequence of letters with the most anagrams in a given file
+unit sub MAIN(
+ IO(Str) $file #= file containing list of words
+ where { given .IO { .r && ( .l || .f) or die "Cannot read from $_" } };
+);
+
+my SetHash %sequences{Str};
+
+for $file.lines -> $word {
+ my $seq = $word.lc.comb(/\w/).sort.join(',') and %sequences{$seq}{$word.lc}++;
+}
+
+my $maxwords = %sequences.values».elems.max;
+
+.fmt('%1$s(%2$d): %2$s').say for %sequences.pairs.grep: *.value.elems == $maxwords
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;
+}
diff --git a/challenge-005/joelle-maslak/perl6/ch-1-readme b/challenge-005/joelle-maslak/perl6/ch-1-readme
deleted file mode 100644
index d3f6e261ab..0000000000
--- a/challenge-005/joelle-maslak/perl6/ch-1-readme
+++ /dev/null
@@ -1,2 +0,0 @@
-The program, including line feeds, is 46 bytes long.
-The output on Unix is 46 pi digits.
diff --git a/challenge-005/john-barrett/perl5/README.md b/challenge-005/john-barrett/perl5/README.md
deleted file mode 100644
index 9a98597b12..0000000000
--- a/challenge-005/john-barrett/perl5/README.md
+++ /dev/null
@@ -1,8 +0,0 @@
-# How I cheated this week...
-
-## Part 1:
-
-You need to run...
-
-`$ perl -MMath::Trig ch-1.pl`
-
diff --git a/challenge-005/rob4t/perl6/ch-1.p6 b/challenge-005/rob4t/perl6/ch-1.p6
new file mode 100644
index 0000000000..d4f5295fbe
--- /dev/null
+++ b/challenge-005/rob4t/perl6/ch-1.p6
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(Str $word, Str $file where *.IO.r = '/usr/share/dict/words') {
+ my $word_bag = $word.lc.comb.Bag;
+
+ my @found_words = $file.IO.lines.grep: {
+ # not the same word
+ .lc ne $word.lc
+ and
+ # look for words AND phrases
+ .lc.words.map({.comb}).flat.Bag eqv $word_bag
+ };
+
+ .say for @found_words;
+}
diff --git a/challenge-005/rob4t/perl6/ch-2.p6 b/challenge-005/rob4t/perl6/ch-2.p6
new file mode 100644
index 0000000000..d705130729
--- /dev/null
+++ b/challenge-005/rob4t/perl6/ch-2.p6
@@ -0,0 +1,11 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(Str $file where *.IO.r = '/usr/share/dict/words' ) {
+ my %a is Bag = $file.IO.lines.map({.words.map({.comb}).flat.Bag});
+
+ my $max_val = %a.values.max;
+ for %a.pairs.grep: *.value == $max_val {
+ say join(": ", .value, .key.kxxv.sort.join);
+ }
+}