diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-03-19 22:38:24 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-03-19 22:38:24 +0000 |
| commit | 25f3fb56d4efd1931f7586253c33dcfa964752f9 (patch) | |
| tree | 0f5f5f084381b248d798b4af99f04dc4a40f59ac | |
| parent | 5395f01802fbf21afed5c5e25a44e46361431be1 (diff) | |
| download | perlweeklychallenge-club-25f3fb56d4efd1931f7586253c33dcfa964752f9.tar.gz perlweeklychallenge-club-25f3fb56d4efd1931f7586253c33dcfa964752f9.tar.bz2 perlweeklychallenge-club-25f3fb56d4efd1931f7586253c33dcfa964752f9.zip | |
w261 - Task 1 & 2
| -rwxr-xr-x | challenge-261/perlboy1967/perl/ch1.pl | 37 | ||||
| -rwxr-xr-x | challenge-261/perlboy1967/perl/ch2.pl | 42 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-261/perlboy1967/perl/ch1.pl b/challenge-261/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..76c62f9511 --- /dev/null +++ b/challenge-261/perlboy1967/perl/ch1.pl @@ -0,0 +1,37 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 261 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-261 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Element Digit Sum +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. + +Write a script to evaluate the absolute difference between element +and digit sum of the given array. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::AllUtils qw(sum); + +sub elementDigitSum (@ints) { + sum(@ints) - sum(split //,join '',@ints); +} + +is(elementDigitSum(1,2,3,45),36); +is(elementDigitSum(1,12,3),9); +is(elementDigitSum(1,2,3,4),0); +is(elementDigitSum(236,416,336,350),1296); + +done_testing; diff --git a/challenge-261/perlboy1967/perl/ch2.pl b/challenge-261/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..dc1b708d01 --- /dev/null +++ b/challenge-261/perlboy1967/perl/ch2.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 261 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-261 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Multiply by Two +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and an integer $start.. + +Write a script to do the followings: + +a) Look for $start in the array @ints, if found multiply the number by 2 +b) If not found stop the process otherwise repeat + +In the end return the final value. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::AllUtils qw(sum); + +sub multiplyByTwo ($i,@ints) { + my %i = map { ($_,1) } @ints; + $i *= 2 while (exists $i{$i}); + return $i; +} + +is(multiplyByTwo(3,5,3,6,1,12),24); +is(multiplyByTwo(1,1,2,4,3),8); +is(multiplyByTwo(2,5,6,7),2); + +done_testing; |
