diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-07-14 09:51:22 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-07-14 09:51:22 -0500 |
| commit | a02cd77ace785533f115eb97cf23081973ebc41c (patch) | |
| tree | c67e3c88391918102701d1fb16e01899c20485de | |
| parent | ed5502f50c8da3dd40260a1b63880ff3089719b4 (diff) | |
| download | perlweeklychallenge-club-a02cd77ace785533f115eb97cf23081973ebc41c.tar.gz perlweeklychallenge-club-a02cd77ace785533f115eb97cf23081973ebc41c.tar.bz2 perlweeklychallenge-club-a02cd77ace785533f115eb97cf23081973ebc41c.zip | |
Week 277 solutions
| -rw-r--r-- | challenge-277/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-277/bob-lied/perl/ch-1.pl | 73 | ||||
| -rw-r--r-- | challenge-277/bob-lied/perl/ch-2.pl | 74 |
3 files changed, 150 insertions, 3 deletions
diff --git a/challenge-277/bob-lied/README b/challenge-277/bob-lied/README index 3d141f3010..280b5bfa87 100644 --- a/challenge-277/bob-lied/README +++ b/challenge-277/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 276 by Bob Lied +Solutions to weekly challenge 277 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-276/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-276/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-277/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-277/bob-lied 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 { }, + }); +} |
