diff options
| -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'; |
