aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-05-12 09:31:17 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-05-12 09:31:17 +0000
commit69c700b93fc33940ad80f38c7e7571d27c050d13 (patch)
treed60477a60dec8234bd742f67ed277c8b0e795da9
parentee69e1eac12ef2e1013923cd1fd9bf1384c98aba (diff)
downloadperlweeklychallenge-club-69c700b93fc33940ad80f38c7e7571d27c050d13.tar.gz
perlweeklychallenge-club-69c700b93fc33940ad80f38c7e7571d27c050d13.tar.bz2
perlweeklychallenge-club-69c700b93fc33940ad80f38c7e7571d27c050d13.zip
w321 - Task 1 & 2
-rwxr-xr-xchallenge-321/perlboy1967/perl/ch1.pl39
-rwxr-xr-xchallenge-321/perlboy1967/perl/ch2.pl36
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-321/perlboy1967/perl/ch1.pl b/challenge-321/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..36a3e5678b
--- /dev/null
+++ b/challenge-321/perlboy1967/perl/ch1.pl
@@ -0,0 +1,39 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 321
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-321#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Distinct Average
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of numbers with even length.
+
+Write a script to return the count of distinct average. The average is
+calculate by removing the minimum and the maximum, then average of the two.
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+use Test2::V0 qw(-no_srand);
+no warnings qw(experimental::signatures);
+
+sub distinctAverage (@ints) {
+ my %f;
+ @ints = sort { $a <=> $b } @ints;
+ $f{(shift(@ints)+pop(@ints))/2}++ while (@ints > 1);
+ $f{$ints[0]}++ if @ints;
+ return (sort { $a <=> $b } values %f)[-1];
+}
+
+is(distinctAverage(1,2,4,3,5,6),3,'Example 1');
+is(distinctAverage(0,2,4,8,3,5),2,'Example 2');
+is(distinctAverage(7,3,1,0,5,9),2,'Example 3');
+is(distinctAverage(1,2,3,4,5),3,'Own example');
+
+done_testing;
diff --git a/challenge-321/perlboy1967/perl/ch2.pl b/challenge-321/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..e8f7b52389
--- /dev/null
+++ b/challenge-321/perlboy1967/perl/ch2.pl
@@ -0,0 +1,36 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 321
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-321#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Backspace Compare
+Submitted by: Mohammad Sajid Anwar
+
+You are given two strings containing zero or more #.
+
+Write a script to return true if the two given strings are same by treating # as backspace.
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+use Test2::V0 qw(-no_srand);
+no warnings qw(experimental::signatures);
+
+use boolean;
+
+sub backspaceCompare ($s1,$s2) {
+ map { 1 while (s/[^#]#//) } $s1,$s2;
+ boolean ($s1 eq $s2);
+}
+
+is(true, backspaceCompare('ab#c','ad#c'),'Example 1');
+is(true, backspaceCompare('ab##','a#b#'),'Example 2');
+is(false,backspaceCompare('a#b','c'),'Example 3');
+
+done_testing;