diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-07-09 17:53:40 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-07-09 17:53:40 +0100 |
| commit | 96580cd62594876fa534d57eb553637433c8ba04 (patch) | |
| tree | ecfea2f0f01a37261e5495d262f76ce88296389c /challenge-277 | |
| parent | d4cb625b21df7b106a6b2272049a5c9842e186ad (diff) | |
| download | perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.tar.gz perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.tar.bz2 perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.zip | |
- Added solutions by Peter Meszaros.
- Added solutions by Steven Wilson.
- Added solutions by Matthew Neleigh.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Reinier Maliepaard.
Diffstat (limited to 'challenge-277')
| -rw-r--r-- | challenge-277/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-277/laurent-rosenfeld/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-277/laurent-rosenfeld/raku/ch-1.raku | 23 | ||||
| -rw-r--r-- | challenge-277/reinier-maliepaard/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-277/reinier-maliepaard/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-277/reinier-maliepaard/perl/ch-2.pl | 39 |
6 files changed, 146 insertions, 0 deletions
diff --git a/challenge-277/laurent-rosenfeld/blog.txt b/challenge-277/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..c5f83d45db --- /dev/null +++ b/challenge-277/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/07/perl-weekly-challenge-277-count-common.html diff --git a/challenge-277/laurent-rosenfeld/perl/ch-1.pl b/challenge-277/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..3c1137e0df --- /dev/null +++ b/challenge-277/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,35 @@ +use warnings; +use feature 'say'; +use Data::Dumper; + +sub count_common { + my (%in1, %in2); + for my $word (@{$_[0]}) { + $in1{$word}++; + } + for my $word (@{$_[1]}) { + $in2{$word}++; + } + my %unique1 = map { $_ => 1 } grep {$in1{$_} == 1} keys %in1; + my %unique2 = map { $_ => 1 } grep {$in2{$_} == 1} keys %in2; + my @intersect; + for my $word (keys %unique1) { + push @intersect, $word if exists $unique2{$word}; + } + return scalar @intersect; +} +my @tests = ( [ [<Perl is my friend>], + [<Perl and Raku are friend>] ], + [ [<Perl is my friend>], + [<Raku is friend of my friend Perl>] ], + [ [<Perl and Python are very similar>], + [<Python is top in guest languages>] ], + [ [<Perl is imperative Lisp is functional>], + [<Crystal is similar to Ruby>] ] + ); +for my $test (@tests) { + say "@{$test->[0]}"; + say "@{$test->[1]}"; + say count_common $test->[0], $test->[1]; + say ""; +} diff --git a/challenge-277/laurent-rosenfeld/raku/ch-1.raku b/challenge-277/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..32ba7f01f7 --- /dev/null +++ b/challenge-277/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,23 @@ +sub count-common (@in1, @in2) { + my $bag-in1 = @in1.Bag; + my $bag-in2 = @in2.Bag; + my $unique1 = grep {$bag-in1{$_} == 1}, $bag-in1.keys; . + my $unique2 = grep {$bag-in2{$_} == 1}, $bag-in2.keys; + return ($unique1 ∩ $unique2).elems; +} + +my @tests = ( <Perl is my friend>, + <Perl and Raku are friend> ), + ( <Perl is my friend>, + <Raku is friend of my friend Perl> ), + ( <Perl and Python are very similar>, + <Python is top in guest languages> ), + ( <Perl is imperative Lisp is functional>, + <Crystal is similar to Ruby> ); + +for @tests -> @test { + say @test[0]; + say @test[1]; + say count-common @test[0], @test[1]; + say ""; +} diff --git a/challenge-277/reinier-maliepaard/blog.txt b/challenge-277/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..0a4f5cb998 --- /dev/null +++ b/challenge-277/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc277 diff --git a/challenge-277/reinier-maliepaard/perl/ch-1.pl b/challenge-277/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..46851c9126 --- /dev/null +++ b/challenge-277/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use List::Compare; + +sub count_common { + + my ($words1, $words2) = @_; + + # Find common elements using List::Compare + my $lc = List::Compare->new($words1, $words2); + my @common_words = $lc->get_intersection; + + my %frequency; + + # Count frequencies of common words in both arrays + foreach my $word (@common_words) { + $frequency{$word}{words1} = scalar ( grep { $_ eq $word } @$words1 ); + $frequency{$word}{words2} = scalar ( grep { $_ eq $word } @$words2 ); + } + + # Filter words that appear exactly once in both arrays + my @filtered_words = grep { $frequency{$_}{words1} == 1 && + $frequency{$_}{words2} == 1 } @common_words; + + return(scalar @filtered_words); +} + +# TESTS + +my (@words1, @words2); + +# Example 1 +@words1 = ("Perl", "is", "my", "friend"); +@words2 = ("Perl", "and", "Raku", "are", "friend"); +print(count_common(\@words1, \@words2), "\n"); # Output: 2 + +# Example 2 +@words1 = ("Perl", "and", "Python", "are", "very", "similar"); +@words2 = ("Python", "is", "top", "in", "guest", "languages"); +print(count_common(\@words1, \@words2), "\n"); # Output: 1 + +# Example 3 +@words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional"); +@words2 = ("Crystal", "is", "similar", "to", "Ruby"); +print(count_common(\@words1, \@words2), "\n"); # Output: 0 diff --git a/challenge-277/reinier-maliepaard/perl/ch-2.pl b/challenge-277/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..34b299344a --- /dev/null +++ b/challenge-277/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use List::Uniq ':all'; +use Math::Combinatorics; + +sub strong_pair { + + # Duplicate values are unnecessary and lead to duplicate pairs, + # so they should be removed: uniq(@_); + + my $c = Math::Combinatorics->new ( count => 2, data => [uniq(@_)], ); + + my $count = 0; + + while ( my @cmb = $c->next_combination ) { + + my $abs = abs($cmb[0] - $cmb[1]); + # finding min(): ($x + $y + abs($x - $y)) / 2 + # https://www.perlmonks.org/?node_id=406883 + $count++ if ( ($abs < ($cmb[0] + $cmb[1] - $abs) / 2) && ($abs > 0) ); + + } + + return ($count); +} + +# TESTS + +my @ints; + +# Example 1 +@ints = (1, 2, 3, 4, 5); +print(strong_pair(@ints), "\n"); # Output: 4 + +# Example 2 +@ints = (5, 7, 1, 7); +print(strong_pair(@ints), "\n"); # Output: 1 |
