From 6e4dc9f568528b10f3f7a47da3e7d2e629e66637 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 9 Jul 2024 09:38:43 +0200 Subject: Add solutions to 277: Count Common & Strong Pair by E. Choroba --- challenge-277/e-choroba/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-277/e-choroba/perl/ch-2.pl | 22 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100755 challenge-277/e-choroba/perl/ch-1.pl create mode 100755 challenge-277/e-choroba/perl/ch-2.pl 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'; -- cgit