diff options
| -rwxr-xr-x | challenge-321/perlboy1967/perl/ch1.pl | 39 | ||||
| -rwxr-xr-x | challenge-321/perlboy1967/perl/ch2.pl | 36 |
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; |
