aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorbagheera-sands <git@sandsscouts.org.uk>2019-04-30 00:56:14 +0100
committerbagheera-sands <git@sandsscouts.org.uk>2019-04-30 00:56:14 +0100
commitfe9360b22574abda0ae267f0ef7a18446d3280f2 (patch)
tree23d9d5746138e722e496ab71881912fe52bc4316 /challenge-005
parentcec6581d6bb64aab7cf23f0ee4f1c9c08702dcb0 (diff)
parent2f5ee8d01e0212d9c8e587014b0df2710e0b0ef1 (diff)
downloadperlweeklychallenge-club-fe9360b22574abda0ae267f0ef7a18446d3280f2.tar.gz
perlweeklychallenge-club-fe9360b22574abda0ae267f0ef7a18446d3280f2.tar.bz2
perlweeklychallenge-club-fe9360b22574abda0ae267f0ef7a18446d3280f2.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-005')
-rw-r--r--challenge-005/adam-russell/blog.txt1
-rw-r--r--challenge-005/adam-russell/perl5/ch-1.pl63
-rw-r--r--challenge-005/adam-russell/perl5/ch-2.pl70
-rw-r--r--challenge-005/andrezgz/perl5/ch-1.pl59
-rw-r--r--challenge-005/andrezgz/perl5/ch-2.pl45
-rw-r--r--challenge-005/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-005/arne-sommer/perl6/ch-1.p623
-rwxr-xr-xchallenge-005/arne-sommer/perl6/ch-2.p625
-rwxr-xr-xchallenge-005/arne-sommer/perl6/ch-2a.p630
-rwxr-xr-xchallenge-005/arne-sommer/perl6/dictionary-lookup17
-rwxr-xr-xchallenge-005/arne-sommer/perl6/dictionary-lookup217
-rwxr-xr-xchallenge-005/arne-sommer/perl6/english.txt61
-rwxr-xr-xchallenge-005/arne-sommer/perl6/maxigrams-error38
-rwxr-xr-xchallenge-005/arne-sommer/perl6/mkdictionary14
-rwxr-xr-xchallenge-005/arne-sommer/perl6/multigrams70
-rw-r--r--challenge-005/athanasius/perl5/ch-1.pl64
-rw-r--r--challenge-005/athanasius/perl5/ch-2.pl75
-rw-r--r--challenge-005/daniel-mantovani/perl5/ch-1.pl79
-rw-r--r--challenge-005/daniel-mantovani/perl5/ch-2.pl94
-rw-r--r--challenge-005/daniel-mantovani/perl5/palabras.txt249387
-rw-r--r--challenge-005/doug-schrag/perl6/ch-1.p626
-rw-r--r--challenge-005/doug-schrag/perl6/ch-2.p644
-rw-r--r--challenge-005/duncan-c-white/README59
-rwxr-xr-xchallenge-005/duncan-c-white/perl5/ch-1.pl45
-rwxr-xr-xchallenge-005/duncan-c-white/perl5/ch-2.pl62
-rw-r--r--challenge-005/e-choroba/README1
-rw-r--r--challenge-005/e-choroba/blog.txt1
-rw-r--r--challenge-005/e-choroba/perl5/ch-1.pl11
-rw-r--r--challenge-005/e-choroba/perl5/ch-2.pl22
-rw-r--r--challenge-005/guillermo-ramos/README1
-rw-r--r--challenge-005/guillermo-ramos/perl5/ch-1.pl37
-rw-r--r--challenge-005/gustavo-chaves/blog.txt1
-rwxr-xr-xchallenge-005/gustavo-chaves/perl5/ch-2b.pl23
-rw-r--r--challenge-005/jaime/README24
-rw-r--r--challenge-005/jaime/perl5/ch-1.pl27
-rw-r--r--challenge-005/jaime/perl5/ch-2.pl16
-rwxr-xr-xchallenge-005/jaldhar-h-vyas/perl5/ch-1.pl45
-rwxr-xr-xchallenge-005/jaldhar-h-vyas/perl5/ch-2.pl45
-rwxr-xr-xchallenge-005/jaldhar-h-vyas/perl6/ch-1.p618
-rwxr-xr-xchallenge-005/jaldhar-h-vyas/perl6/ch-2.p619
-rw-r--r--challenge-005/jo-christian-oterhals/blog.txt1
-rw-r--r--challenge-005/jo-christian-oterhals/perl6/ch-1.p64
-rw-r--r--challenge-005/jo-christian-oterhals/perl6/ch-2.p610
-rw-r--r--challenge-005/joelle-maslak/blog.txt1
-rwxr-xr-xchallenge-005/joelle-maslak/perl5/ch-1.pl60
-rwxr-xr-xchallenge-005/joelle-maslak/perl5/ch-2.pl58
-rwxr-xr-xchallenge-005/joelle-maslak/perl6/ch-1.p640
-rwxr-xr-xchallenge-005/joelle-maslak/perl6/ch-2.p646
-rwxr-xr-xchallenge-005/john-barrett/perl5/ch-1.pl20
-rwxr-xr-xchallenge-005/john-barrett/perl5/ch-2.pl27
-rw-r--r--challenge-005/kian-meng-ang/ch-1.pl40
-rw-r--r--challenge-005/kian-meng-ang/ch-2.pl44
-rw-r--r--challenge-005/lars-balker/perl5/ch-1.pl55
-rw-r--r--challenge-005/lars-balker/perl5/ch-2.pl25
-rw-r--r--challenge-005/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-005/laurent-rosenfeld/perl5/ch-1.pl13
-rw-r--r--challenge-005/laurent-rosenfeld/perl5/ch-1a.pl14
-rw-r--r--challenge-005/laurent-rosenfeld/perl5/ch-1b.pl15
-rw-r--r--challenge-005/laurent-rosenfeld/perl5/ch-2.pl27
-rw-r--r--challenge-005/laurent-rosenfeld/perl6/ch-1.p67
-rw-r--r--challenge-005/laurent-rosenfeld/perl6/ch-1a.p66
-rw-r--r--challenge-005/laurent-rosenfeld/perl6/ch-1b.p66
-rw-r--r--challenge-005/laurent-rosenfeld/perl6/ch-2.p618
-rw-r--r--challenge-005/laurent-rosenfeld/perl6/ch-2a.p610
-rw-r--r--challenge-005/mark-senn/blog.txt1
-rw-r--r--challenge-005/mark-senn/perl6/ch-1.p688
-rw-r--r--challenge-005/mark-senn/perl6/ch-2.p688
-rw-r--r--challenge-005/ruben-westerberg/README11
-rw-r--r--challenge-005/ruben-westerberg/perl5/.ch-2.pl.swpbin0 -> 12288 bytes
-rwxr-xr-xchallenge-005/ruben-westerberg/perl5/ch-1.pl55
-rwxr-xr-xchallenge-005/ruben-westerberg/perl5/ch-2.pl24
-rw-r--r--challenge-005/ruben-westerberg/perl5/p5.txt135
-rw-r--r--challenge-005/ruben-westerberg/perl5/p6.txt720
-rw-r--r--challenge-005/ruben-westerberg/perl6/.ch-1.p6.swpbin0 -> 12288 bytes
-rw-r--r--challenge-005/ruben-westerberg/perl6/.ch-2.p6.swpbin0 -> 12288 bytes
-rw-r--r--challenge-005/ruben-westerberg/perl6/.precomp/.lock0
-rw-r--r--challenge-005/ruben-westerberg/perl6/.precomp/E8252BAA8CCA5C482BDD1088C325C513F7B95D46/8E/8EC489C474F22D0612C0E178FCE69EABDC1889BAbin0 -> 10435 bytes
-rw-r--r--challenge-005/ruben-westerberg/perl6/.precomp/E8252BAA8CCA5C482BDD1088C325C513F7B95D46/8E/8EC489C474F22D0612C0E178FCE69EABDC1889BA.repo-id1
-rw-r--r--challenge-005/ruben-westerberg/perl6/anagram.pm634
-rwxr-xr-xchallenge-005/ruben-westerberg/perl6/ch-1.p611
-rwxr-xr-xchallenge-005/ruben-westerberg/perl6/ch-2.p66
-rw-r--r--challenge-005/ruben-westerberg/perl6/input-words.txt1
-rw-r--r--challenge-005/ruben-westerberg/perl6/short.txt3
-rw-r--r--challenge-005/ruben-westerberg/perl6/words_alpha.zipbin0 -> 936640 bytes
-rw-r--r--challenge-005/ruben-westerberg/words_alpha.txt370099
-rw-r--r--challenge-005/simon-proctor/blog.txt1
-rwxr-xr-xchallenge-005/simon-proctor/perl6/ch-1.p622
-rwxr-xr-xchallenge-005/simon-proctor/perl6/ch-2.p648
-rw-r--r--challenge-005/simon-proctor/perl6/lib/Anagrams.pm617
89 files changed, 622643 insertions, 30 deletions
diff --git a/challenge-005/adam-russell/blog.txt b/challenge-005/adam-russell/blog.txt
new file mode 100644
index 0000000000..cae252cc33
--- /dev/null
+++ b/challenge-005/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/1433.html
diff --git a/challenge-005/adam-russell/perl5/ch-1.pl b/challenge-005/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..e9293ff9a4
--- /dev/null
+++ b/challenge-005/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+##
+# Write a program which prints out all anagrams for a given word.
+##
+use constant DICTIONARY => "/usr/share/dict/words";
+my %word_product;
+my %letter_factor = (
+ e => 2,
+ t => 3,
+ a => 5,
+ o => 7,
+ i => 11,
+ n => 13,
+ s => 17,
+ h => 19,
+ r => 23,
+ d => 29,
+ l => 31,
+ c => 37,
+ u => 41,
+ m => 43,
+ w => 47,
+ f => 53,
+ g => 59,
+ y => 61,
+ p => 67,
+ b => 71,
+ v => 73,
+ k => 79,
+ j => 83,
+ x => 89,
+ q => 97,
+ z => 101
+);
+
+##
+# Main
+##
+my $test_word = lc($ARGV[0]);
+$test_word =~ tr/-//d;
+my @letters = split(//, $test_word);
+my $test_word_product = 1;
+map {$test_word_product *= $_} map{$letter_factor{$_}} @letters;
+open(WORDS, DICTIONARY);
+while(<WORDS>){
+ chomp($_);
+ my $word = lc($_);
+ $word =~ tr/-//d;
+ @letters = split(//, $word);
+ my $product = 1;
+ map {$product *= $_} map{$letter_factor{$_}} @letters;
+ $word_product{$word} = $product;
+}
+close(WORDS);
+delete($word_product{$test_word});
+my @anagrams = grep {$word_product{$_} == $test_word_product} keys(%word_product);
+if(@anagrams){
+ print "$test_word: " . join(" ", @anagrams) . "\n";
+}
+else{
+ print "$test_word: No anagrams found.\n";
+}
diff --git a/challenge-005/adam-russell/perl5/ch-2.pl b/challenge-005/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..577e6bc354
--- /dev/null
+++ b/challenge-005/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+##
+# Write a program to find the sequence of characters that has the most anagrams.
+##
+my %word_product;
+my %letter_factor = (
+ e => 2,
+ t => 3,
+ a => 5,
+ o => 7,
+ i => 11,
+ n => 13,
+ s => 17,
+ h => 19,
+ r => 23,
+ d => 29,
+ l => 31,
+ c => 37,
+ u => 41,
+ m => 43,
+ w => 47,
+ f => 53,
+ g => 59,
+ y => 61,
+ p => 67,
+ b => 71,
+ v => 73,
+ k => 79,
+ j => 83,
+ x => 89,
+ q => 97,
+ z => 101
+);
+my %factor_letter = reverse(%letter_factor);
+
+sub prime_factor{
+ my $x = shift(@_);
+ my @factors;
+ for (my $y = 2; $y <= $x; $y++){
+ next if $x % $y;
+ $x /= $y;
+ push @factors, $y;
+ redo;
+ }
+ return @factors;
+}
+
+##
+# Main
+##
+while(<>){
+ chomp($_);
+ my $word = lc($_);
+ $word =~ tr/-//d;
+ my @letters = split(//, $word);
+ my $product = 1;
+ map {$product *= $_} map{$letter_factor{$_}} @letters;
+ $word_product{$word} = $product;
+}
+
+my @words = keys(%word_product);
+my %product_count;
+for my $word (@words){
+ $product_count{$word_product{$word}} ||= 0;
+ $product_count{$word_product{$word}}++;
+}
+
+my @sorted = sort {$product_count{$b} <=> $product_count{$a}} keys %product_count;
+print join(" ", map {$factor_letter{$_}} prime_factor($sorted[0])) . "\n";
diff --git a/challenge-005/andrezgz/perl5/ch-1.pl b/challenge-005/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..f054952971
--- /dev/null
+++ b/challenge-005/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-005/
+# Challenge #1
+# Write a program which prints out all anagrams for a given word.
+# For more information about Anagram, please check this wikipedia page.
+# https://en.wikipedia.org/wiki/Anagram
+
+# An anagram may be a phrase, but I limit this code only to words of the same length as the given one,
+# because even if I can make a phrase (string of words using the considered letters) it may not have sense.
+
+# I've reused code from the previous challenge
+# https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-004/andrezgz/perl5/ch-2.pl
+
+use strict;
+use warnings;
+
+die "Usage: ch-1.pl <words_file> <word>" if scalar(@ARGV) < 2;
+
+my $words_file = $ARGV[0];
+my $given_word = $ARGV[1];
+
+open(my $fh, "<", $words_file) or die "Could not open words file '$words_file': $!";
+
+while( my $word = <$fh> ) {
+ chomp $word; #remove new line trailing string
+ print $word.$/ if is_word_anagram( lc $word , lc $given_word);
+}
+close $fh;
+
+#Return 1 if $word is an anagram for $given_word
+sub is_word_anagram {
+ my ( $word, $given_word ) = @_;
+
+ return 0 if ( length $word != length $given_word ); #all letters must be used in a word anagram
+ return 0 if ( $word eq $given_word ); #the word must not be the given one
+
+ my %letters = get_hashed_letters($given_word);
+
+ foreach my $l (split //,$word ){
+ return 0 unless (exists $letters{ $l } && $letters{ $l } > 0);
+ $letters{ $l }--;
+ }
+ return 1;
+}
+
+{
+ my %letters; #generate the hash only once
+
+ #Make a hash with available letters and quantity
+ sub get_hashed_letters {
+ my ( $given_word ) = @_;
+ return %letters if (keys %letters);
+ for my $l (split //,$given_word ){
+ $letters{ lc($l) }++;
+ }
+ return %letters
+ }
+}
diff --git a/challenge-005/andrezgz/perl5/ch-2.pl b/challenge-005/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..608cb9c1e4
--- /dev/null
+++ b/challenge-005/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-005/
+# Challenge #2
+# Write a program to find the sequence of characters that has the most anagrams.
+
+# An anagram may be a phrase, but I limit this code only to words of the same length as the given one,
+# because even if I can make a phrase (string of words using the considered letters) it may not have sense.
+
+use strict;
+use warnings;
+
+die "Usage: ch-2.pl <words_file>" if scalar(@ARGV) != 1;
+
+my $words_file = $ARGV[0];
+
+open(my $fh, "<", $words_file) or die "Could not open words file '$words_file': $!";
+
+my %anagrams;
+my %candidates;
+my $max_anagrams = 0;
+
+while( my $word = <$fh> ) {
+ chomp $word; #remove new line trailing string
+
+ my $k = join( '', sort split //, lc $word); #identifier for words with same letters
+ push @{$anagrams{$k}}, $word;
+
+ my $k_anagrams = @{$anagrams{$k}};
+ next if ($k_anagrams < $max_anagrams); # not a candidate right now
+ $candidates{$k} = 1; # it's a candidate
+ $max_anagrams = $k_anagrams if ( $k_anagrams > $max_anagrams ); # the best candidate at the moment
+
+}
+close $fh;
+
+print "Sequence of characters with the most anagrams ($max_anagrams)".$/;
+
+#Check only candidates to avoid looping over the complete %anagrams hash
+foreach my $k (keys %candidates) {
+ next if @{$anagrams{$k}} != $max_anagrams;
+ print join( ',', split //, $k)
+ . " => "
+ . join(",", sort {lc($a) cmp lc($b)} @{$anagrams{$k}}) . $/;
+}
diff --git a/challenge-005/arne-sommer/blog.txt b/challenge-005/arne-sommer/blog.txt
new file mode 100644
index 0000000000..41b8948747
--- /dev/null
+++ b/challenge-005/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://perl6.eu/anagrams.html
diff --git a/challenge-005/arne-sommer/perl6/ch-1.p6 b/challenge-005/arne-sommer/perl6/ch-1.p6
new file mode 100755
index 0000000000..660283c160
--- /dev/null
+++ b/challenge-005/arne-sommer/perl6/ch-1.p6
@@ -0,0 +1,23 @@
+#! /usr/bin/env perl6
+
+unit sub MAIN (Str $word is copy where $word !~~ /\W/,
+ :$dictionary where $dictionary.IO.r = "/usr/share/dict/british-english");
+
+$word .= lc;
+
+my $dict = get-dictionary($dictionary);
+
+print "Anagrams:";
+
+for $word.comb.permutations>>.join.unique -> $candidate
+{
+ # next if $candidate eq $word;
+ print " $candidate" if $dict{$candidate};
+}
+print "\n";
+
+sub get-dictionary ($file where $file.IO.r) is export
+{
+ return $file.IO.lines.grep(* !~~ /\W/)>>.lc.Set;
+}
+
diff --git a/challenge-005/arne-sommer/perl6/ch-2.p6 b/challenge-005/arne-sommer/perl6/ch-2.p6
new file mode 100755
index 0000000000..b70495d4a5
--- /dev/null
+++ b/challenge-005/arne-sommer/perl6/ch-2.p6
@@ -0,0 +1,25 @@
+#! /usr/bin/env perl6
+
+unit sub MAIN (Str :$dictionary where $dictionary.IO.r = "dict-UK.txt");
+
+my $dict = get-dictionary($dictionary);
+
+my %count;
+
+%count{ .comb.sort.join }++ for $dict.keys;
+
+my $ma