From c6325f49e06c4ad11136874faac18e2dcd3eeddf Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Mon, 22 Apr 2019 12:35:47 +1000 Subject: Week 5 Challenge 1 solution --- challenge-005/fjwhittle/perl6/ch-1.p6 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenge-005/fjwhittle/perl6/ch-1.p6 (limited to 'challenge-005') 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; -- cgit From 8b452eba2700aadc92dbdb3ec55851fbac7e4fc4 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Mon, 22 Apr 2019 15:34:30 +1000 Subject: Week 5 Challenge 2 solution --- challenge-005/fjwhittle/perl6/ch-2.p6 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-005/fjwhittle/perl6/ch-2.p6 (limited to 'challenge-005') 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 -- cgit From 0f012b9d734d64ce45df5b7be4919d328590e5f4 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Mon, 22 Apr 2019 16:45:33 +1000 Subject: Week 5 blog post. --- challenge-005/fjwhittle/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-005/fjwhittle/blog.txt (limited to 'challenge-005') 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 -- cgit From 01a67a7f00c490a44549e09fbd4c150ea68828a7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 22 Apr 2019 10:28:50 +0100 Subject: - Removed unwanted files. --- challenge-005/joelle-maslak/perl6/ch-1-readme | 2 -- challenge-005/john-barrett/perl5/README.md | 8 -------- 2 files changed, 10 deletions(-) delete mode 100644 challenge-005/joelle-maslak/perl6/ch-1-readme delete mode 100644 challenge-005/john-barrett/perl5/README.md (limited to 'challenge-005') 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` - -- cgit From dd93f741588af128748c55bf327bf2ed9bf75b7c Mon Sep 17 00:00:00 2001 From: Rob4t Date: Mon, 22 Apr 2019 14:39:05 +0200 Subject: solution for challenge 005 --- challenge-005/rob4t/perl6/ch-1.p6 | 16 ++++++++++++++++ challenge-005/rob4t/perl6/ch-2.p6 | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge-005/rob4t/perl6/ch-1.p6 create mode 100644 challenge-005/rob4t/perl6/ch-2.p6 (limited to 'challenge-005') diff --git a/challenge-005/rob4t/perl6/ch-1.p6 b/challenge-005/rob4t/perl6/ch-1.p6 new file mode 100644 index 0000000000..5e12e50898 --- /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 = '/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); + } +} -- cgit From 38a2dd0cc58272e1033b7a5c20c040e0e46404d3 Mon Sep 17 00:00:00 2001 From: Rob4t Date: Mon, 22 Apr 2019 14:44:13 +0200 Subject: check if file is readable --- challenge-005/rob4t/perl6/ch-1.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-005') diff --git a/challenge-005/rob4t/perl6/ch-1.p6 b/challenge-005/rob4t/perl6/ch-1.p6 index 5e12e50898..d4f5295fbe 100644 --- a/challenge-005/rob4t/perl6/ch-1.p6 +++ b/challenge-005/rob4t/perl6/ch-1.p6 @@ -1,7 +1,7 @@ #!/usr/bin/env perl6 use v6; -sub MAIN(Str $word, Str $file = '/usr/share/dict/words') { +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: { -- cgit 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 (limited to 'challenge-005') 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