diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-07-09 09:38:43 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-07-09 09:38:43 +0200 |
| commit | 6e4dc9f568528b10f3f7a47da3e7d2e629e66637 (patch) | |
| tree | 68fee625249778960e48b678c9bc3f56f967aed1 | |
| parent | ed5502f50c8da3dd40260a1b63880ff3089719b4 (diff) | |
| download | perlweeklychallenge-club-6e4dc9f568528b10f3f7a47da3e7d2e629e66637.tar.gz perlweeklychallenge-club-6e4dc9f568528b10f3f7a47da3e7d2e629e66637.tar.bz2 perlweeklychallenge-club-6e4dc9f568528b10f3f7a47da3e7d2e629e66637.zip | |
Add solutions to 277: Count Common & Strong Pair by E. Choroba
| -rwxr-xr-x | challenge-277/e-choroba/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-277/e-choroba/perl/ch-2.pl | 22 |
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-277/e-choroba/perl/ch-1.pl b/challenge-277/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..8553d1a0a5 --- /dev/null +++ b/challenge-277/e-choroba/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub count_common($words1, $words2) { + my %freq; + ++$freq{$_}[0] for @$words1; + ++$freq{$_}[1] for @$words2; + return grep 1 == ($freq{$_}[0] // 0) + && 1 == ($freq{$_}[1] // 0), keys %freq +} + +use Test::More tests => 3; + +is count_common(['Perl', 'is', 'my', 'friend'], + ['Perl', 'and', 'Raku', 'are', 'friend']), + 2, 'Example 1'; + +is count_common(['Perl', 'and', 'Python', 'are', 'very', 'similar'], + ['Python', 'is', 'top', 'in', 'guest', 'languages']), + 1, 'Example 2'; + +is count_common(['Perl', 'is', 'imperative', 'Lisp', 'is', 'functional'], + ['Crystal', 'is', 'similar', 'to', 'Ruby']), + 0, 'Example 3'; diff --git a/challenge-277/e-choroba/perl/ch-2.pl b/challenge-277/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..b8a911550b --- /dev/null +++ b/challenge-277/e-choroba/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub strong_pair(@ints) { + @ints = sort { $a <=> $b } @ints; + my %strong; + for my $i (0 .. $#ints - 1) { + for my $j ($i + 1 .. $#ints) { + my $diff = $ints[$j] - $ints[$i]; + undef $strong{"@ints[$i, $j]"} if 0 < $diff + && $diff < $ints[$i]; + } + } + return scalar keys %strong +} + +use Test::More tests => 2; + +is strong_pair(1, 2, 3, 4, 5), 4, 'Example 1'; +is strong_pair(5, 7, 1, 7), 1, 'Example 2'; |
