aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/bob-lied/perl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-277/bob-lied/perl')
-rw-r--r--challenge-277/bob-lied/perl/ch-1.pl73
-rw-r--r--challenge-277/bob-lied/perl/ch-2.pl74
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-277/bob-lied/perl/ch-1.pl b/challenge-277/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..8fa1216986
--- /dev/null
+++ b/challenge-277/bob-lied/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge 277 Task 1 Count Common
+#=============================================================================
+# You are given two array of strings, @words1 and @words2.
+# Write a script to return the count of words that appears in both
+# arrays exactly once.
+# Example 1 Input: @words1 = ("Perl", "is", "my", "friend")
+# @words2 = ("Perl", "and", "Raku", "are", "friend")
+# Output: 2
+# The words "Perl" and "friend" appear once in each array.
+# Example 2 Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar")
+# @words2 = ("Python", "is", "top", "in", "guest", "languages")
+# Output: 1
+# Example 3 Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional")
+# @words2 = ("Crystal", "is", "similar", "to", "Ruby")
+# Output: 0
+#=============================================================================
+
+use v5.40;
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+my $Benchmark = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
+exit(!runTest()) if $DoTest;
+exit( runBenchmark($Benchmark) ) if $Benchmark;
+
+my @words1 = split('', shift @ARGV);
+my @words2 = split('', shift @ARGV);
+
+say countCommon(\@words1, \@words2);
+
+sub countCommon($words1, $words2)
+{
+ use List::MoreUtils qw/frequency/;
+ my %common = frequency $words1->@*;
+
+ for ( keys %common ) { delete $common{$_} if $common{$_} != 1 }
+
+ for ( $words2->@* ) { $common{$_}++ if exists $common{$_} }
+
+ return scalar( grep { $_ == 2 } values %common);
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( countCommon([ qw(Perl is my friend) ],
+ [ qw(Perl and Raku are friend) ]), 2, "Example 1");
+ is( countCommon([ qw(Perl and Python are very similar) ],
+ [ qw(Python is top in guest languages) ]), 1, "Example 2");
+ is( countCommon([ qw(Perl is imperative Lisp is functional) ],
+ [ qw(Crystal is similar to Ruby) ]), 0, "Example 3");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}
+
diff --git a/challenge-277/bob-lied/perl/ch-2.pl b/challenge-277/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..a2cd7f2b8c
--- /dev/null
+++ b/challenge-277/bob-lied/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge 277 Task 2 Strong Pairs
+#=============================================================================
+# You are given an array of integers, @ints.
+# Write a script to return the count of all strong pairs in the given array.
+# A pair of integers x and y is called strong pair if
+# it satisfies: 0 < |x - y| < min(x, y).
+# Example 1 Input: @ints = (1, 2, 3, 4, 5)
+# Ouput: 4
+# Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5)
+# Example 2 Input: @ints = (5, 7, 1, 7)
+# Ouput: 1
+# Strong Pairs: (5, 7)
+#=============================================================================
+
+use v5.40;
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+my $Benchmark = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
+exit(!runTest()) if $DoTest;
+exit( runBenchmark($Benchmark) ) if $Benchmark;
+
+say strongPair(@ARGV);
+
+sub strongPair(@ints)
+{
+ use List::Util qw/min uniqnum/;
+
+ # Remove duplicate values
+ @ints = uniqnum sort { $a <=> $b } @ints;
+
+ my $count = 0;
+ while ( defined(my $first = shift @ints) )
+ {
+ for my $second ( @ints )
+ {
+ $count++ if abs($first - $second) < min($first, $second);
+ if ( $Verbose )
+ {
+ my $abs = abs($first - $second);
+ my $min = min($first, $second);
+ say "($first,$second): abs=$abs min=$min count=$count";
+ }
+ }
+ }
+ return $count;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( strongPair(1,2,3,4,5), 4, "Example 1");
+ is( strongPair(5,7,1,7 ), 1, "Example 2");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}