diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-28 18:19:55 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-28 18:19:55 +0100 |
| commit | 4ea279abda9a15890fc858acb8c2d80746c27926 (patch) | |
| tree | ffe5201363b0d1a8908396116e4be3058dba212a /challenge-005 | |
| parent | 7e738cac946a6c0629c1614247cf03fddbe356da (diff) | |
| download | perlweeklychallenge-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.pl | 13 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl5/ch-1a.pl | 14 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl5/ch-1b.pl | 15 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl5/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl6/ch-1.p6 | 7 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl6/ch-1a.p6 | 6 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl6/ch-1b.p6 | 6 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl6/ch-2.p6 | 18 | ||||
| -rw-r--r-- | challenge-005/laurent-rosenfeld/perl6/ch-2a.p6 | 10 |
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; |
