From 6066e198454df3c3db2740708c880aa48d27a25b Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 8 Jul 2024 06:14:12 +0000 Subject: w277 - Task 1 & 2 --- challenge-277/perlboy1967/perl/ch1.pl | 43 +++++++++++++++++++++++++++++++++++ challenge-277/perlboy1967/perl/ch2.pl | 39 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100755 challenge-277/perlboy1967/perl/ch1.pl create mode 100755 challenge-277/perlboy1967/perl/ch2.pl diff --git a/challenge-277/perlboy1967/perl/ch1.pl b/challenge-277/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..1ced4da647 --- /dev/null +++ b/challenge-277/perlboy1967/perl/ch1.pl @@ -0,0 +1,43 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 277 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-277 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Count Common +Submitted by: Mohammad Sajid Anwar + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub countCommon { + my ($arW1,$arW2,%f1,%f2,%fc) = @_; + map { $f1{$_}++ } @$arW1; + map { $f2{$_}++ } @$arW2; + map { $fc{$_}++ } + (grep { $f1{$_} == 1 } keys %f1), + (grep { $f2{$_} == 1 } keys %f2); + scalar grep { $_ == 2 } values %fc; +} + +is(countCommon([qw{Perl is my friend}], + [qw{Perl and Raku are friend}]),2); +is(countCommon([qw{Perl and Python are very similar}], + [qw{Python is top in guest languages}]),1); +is(countCommon([qw{Perl is imperative Lisp is functional}], + [qw{Crystal is similar to Ruby}]),0); + +done_testing; diff --git a/challenge-277/perlboy1967/perl/ch2.pl b/challenge-277/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..1b4ee2cbdb --- /dev/null +++ b/challenge-277/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 277 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-277 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Strong Pair +Submitted by: Mohammad Sajid Anwar + +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). + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +use Algorithm::Combinatorics qw(combinations); +use List::AllUtils qw(min uniq); + +sub strongPair (@ints) { + @ints = uniq(@ints); + scalar grep { abs($$_[0]-$$_[1]) < min(@$_) } combinations(\@ints,2); +} + +is(strongPair(1..5),4); +is(strongPair(5,7,1,7),1); + +done_testing; -- cgit From ddf1af8b36c77f9ebe1175fdfa711505f1264c0a Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 8 Jul 2024 06:39:29 +0000 Subject: Task 1, shorter code using "singleton()" --- challenge-277/perlboy1967/perl/ch1.pl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/challenge-277/perlboy1967/perl/ch1.pl b/challenge-277/perlboy1967/perl/ch1.pl index 1ced4da647..0cfaa4bcb2 100755 --- a/challenge-277/perlboy1967/perl/ch1.pl +++ b/challenge-277/perlboy1967/perl/ch1.pl @@ -23,14 +23,12 @@ use common::sense; use Test2::V0 qw(-no_srand); +use List::AllUtils qw(singleton); + sub countCommon { - my ($arW1,$arW2,%f1,%f2,%fc) = @_; - map { $f1{$_}++ } @$arW1; - map { $f2{$_}++ } @$arW2; - map { $fc{$_}++ } - (grep { $f1{$_} == 1 } keys %f1), - (grep { $f2{$_} == 1 } keys %f2); - scalar grep { $_ == 2 } values %fc; + my %f; + $f{$_}++ for (singleton(@{$_[0]}),singleton(@{$_[1]})); + grep { $_ == 2 } values %f; } is(countCommon([qw{Perl is my friend}], -- cgit