aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-28 18:19:55 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-28 18:19:55 +0100
commit4ea279abda9a15890fc858acb8c2d80746c27926 (patch)
treeffe5201363b0d1a8908396116e4be3058dba212a /challenge-005
parent7e738cac946a6c0629c1614247cf03fddbe356da (diff)
downloadperlweeklychallenge-club-4ea279abda9a15890fc858acb8c2d80746c27926.tar.gz
perlweeklychallenge-club-4ea279abda9a15890fc858acb8c2d80746c27926.tar.bz2
perlweeklychallenge-club-4ea279abda9a15890fc858acb8c2d80746c27926.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-005')
-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
9 files changed, 116 insertions, 0 deletions
diff --git a/challenge-005/laurent-rosenfeld/perl5/ch-1.pl b/challenge-005/laurent-rosenfeld/perl5/ch-1.pl
new file mode 100644
index 0000000000..19f53a6bc7
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl5/ch-1.pl
@@ -0,0 +1,13 @@
+use strict;
+use warnings;
+use feature 'say';
+sub is_anagram {
+ my ($word1, $word2) = @_;
+ return 0 if length $word1 != length $word2;
+ my $letters1 = join "", sort split "", $word1;
+ my $letters2 = join "", sort split "", $word2;
+ return $letters1 eq $letters2
+}
+for ( [qw / ban bane/], [qw /post post/], [qw / post spot /], [qw /post spot/], [qw / pots spot/], [qw /pots taps/] ) {
+ say "$$_[0] $$_[1]:\t", is_anagram($$_[0], $$_[1]) ? "True" : "False";
+}
diff --git a/challenge-005/laurent-rosenfeld/perl5/ch-1a.pl b/challenge-005/laurent-rosenfeld/perl5/ch-1a.pl
new file mode 100644
index 0000000000..20b000f5f9
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl5/ch-1a.pl
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use feature 'say';
+sub is_anagram {
+ my ($word1, $word2) = @_;
+ return 0 if length $word1 != length $word2;
+ my $letters1 = join "", sort split "", $word1;
+ my $letters2 = join "", sort split "", $word2;
+ return $letters1 eq $letters2
+}
+my $word = "post";
+for (qw /past post spot tops taps pots top/) {
+ say "$word $_\t", is_anagram($word, $_) ? "True" : "False";
+}
diff --git a/challenge-005/laurent-rosenfeld/perl5/ch-1b.pl b/challenge-005/laurent-rosenfeld/perl5/ch-1b.pl
new file mode 100644
index 0000000000..1d31cfb414
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl5/ch-1b.pl
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+use feature 'say';
+sub is_anagram {
+ my ($word1, $word2) = @_;
+ return 0 if length $word1 != length $word2;
+ my $letters1 = join "", sort split "", $word1;
+ my $letters2 = join "", sort split "", $word2;
+ return $letters1 eq $letters2
+}
+my $word = "post";
+while (<>) {
+ chomp;
+ say if is_anagram $word, $_;
+}
diff --git a/challenge-005/laurent-rosenfeld/perl5/ch-2.pl b/challenge-005/laurent-rosenfeld/perl5/ch-2.pl
new file mode 100644
index 0000000000..d493ea4b5a
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl5/ch-2.pl
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use feature 'say';
+
+my %words; # our HoA
+my $file_in = "words.txt";
+open my $IN, "<", $file_in or die "Ouverture impossible $file_in $!";
+while (my $word = <$IN>) {
+ next unless $word =~ /\w/; # skipping empty lines if any
+ $word =~ s/\s+$//; # removing trailing spaces, new lines and carriage returns (if any)
+ next if length $word < 3;
+ my $key = join '', sort split //, $word; # normalizing the word for the hash key
+ push @{$words{$key}}, $word; # storing the word in the HoA
+}
+close $IN;
+my @max_anagrams;
+my $max = 5;
+for my $key (keys %words) {
+ next if @{$words{$key}} < $max;
+ if (@{$words{$key}} == $max) {
+ push @max_anagrams, $key;
+ } else {
+ @max_anagrams = ($key);
+ $max = scalar @{$words{$key}};
+ }
+}
+say "$_:\t @{$words{$_}}" for (@max_anagrams);
diff --git a/challenge-005/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-005/laurent-rosenfeld/perl6/ch-1.p6
new file mode 100644
index 0000000000..e63a980a78
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl6/ch-1.p6
@@ -0,0 +1,7 @@
+sub is-anagram (Str $word1, Str $word2) {
+ return False if $word1.chars != $word2.chars;
+ return $word1.comb.sort eq $word2.comb.sort;
+}
+for <ban bane post stop pots stop post pots pots taps> -> $w1, $w2 {
+ say "$w1 $w2:\t", is-anagram $w1, $w2;
+}
diff --git a/challenge-005/laurent-rosenfeld/perl6/ch-1a.p6 b/challenge-005/laurent-rosenfeld/perl6/ch-1a.p6
new file mode 100644
index 0000000000..0e4ae68e5f
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl6/ch-1a.p6
@@ -0,0 +1,6 @@
+sub is-anagram (Str $word1, Str $word2) {
+ return $word1.comb.Bag === $word2.comb.Bag;
+}
+for <ban bane post stop pots stop post pots pots taps> -> $w1, $w2 {
+ say "$w1 $w2:\t", is-anagram $w1, $w2;
+}
diff --git a/challenge-005/laurent-rosenfeld/perl6/ch-1b.p6 b/challenge-005/laurent-rosenfeld/perl6/ch-1b.p6
new file mode 100644
index 0000000000..5abaa0a687
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl6/ch-1b.p6
@@ -0,0 +1,6 @@
+sub infix:<ana> (Str $word1, Str $word2) {
+ return $word1.comb.Bag === $word2.comb.Bag;
+}
+for <ban bane post stop pots stop post pots pots taps> -> $w1, $w2 {
+ say "$w1 $w2:\t", $w1 ana $w2;
+}
diff --git a/challenge-005/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-005/laurent-rosenfeld/perl6/ch-2.p6
new file mode 100644
index 0000000000..5874af92d7
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl6/ch-2.p6
@@ -0,0 +1,18 @@
+my %words;
+for "words.txt".IO.lines -> $line {
+ next unless $line ~~ /\w/; # skipping empty lines if any
+ $line ~~ s/\s+$//; # removing trailing spaces if any
+ next if $line.chars < 3;
+ my $key = $line.comb.sort.join('');
+ push %words{$key}, $line;
+}
+my @max-anagrams;
+my $max = 5;
+for %words.keys -> $key {
+ given %words{$key}.elems {
+ when $_ < $max { next }
+ when $_ == $max { @max-anagrams.push($key) }
+ default { @max-anagrams = $key,; $max = $_}
+ }
+}
+say "$_:\t %words{$_}" for (@max-anagrams);
diff --git a/challenge-005/laurent-rosenfeld/perl6/ch-2a.p6 b/challenge-005/laurent-rosenfeld/perl6/ch-2a.p6
new file mode 100644
index 0000000000..372496cec5
--- /dev/null
+++ b/challenge-005/laurent-rosenfeld/perl6/ch-2a.p6
@@ -0,0 +1,10 @@
+my %words;
+for "words.txt".IO.lines -> $line {
+ next unless $line ~~ /\w/; # skipping empty lines if any
+ $line ~~ s/\s+$//; # removing trailing spaces if any
+ next if $line.chars < 3;
+ my $key = $line.comb.sort.join('');
+ push %words{$key}, $line;
+}
+my $max = max map { .elems }, values %words;
+say "$_:\t %words{$_}" if %words{$_}.elems == $max for %words.keys;