diff options
| -rwxr-xr-x | challenge-252/perlboy1967/perl/ch1.pl | 38 | ||||
| -rwxr-xr-x | challenge-252/perlboy1967/perl/ch2.pl | 40 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-252/perlboy1967/perl/ch1.pl b/challenge-252/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..e8ae528469 --- /dev/null +++ b/challenge-252/perlboy1967/perl/ch1.pl @@ -0,0 +1,38 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 252 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-252 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Special Numbers +Submitted by: Mohammad S Anwar + +You are given an array of integers, @ints. + +Write a script to find the sum of the squares of all special elements of the given array. + +|| An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +|| Where n is the length of the given array. Also the array is 1-indexed for the task. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::Util qw(sum0); + +sub specialNumbers (@ints) { + my $n = $#ints + 1; + sum0 map { $ints[$_ - 1] ** 2 } grep { $n % $_ == 0 } 1 .. $n; +} + +is(specialNumbers(1,2,3,4),21); +is(specialNumbers(2,7,1,19,18,3),63); + +done_testing; diff --git a/challenge-252/perlboy1967/perl/ch2.pl b/challenge-252/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..225f32ac2a --- /dev/null +++ b/challenge-252/perlboy1967/perl/ch2.pl @@ -0,0 +1,40 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 252 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-252 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Unique Sum Zero +Submitted by: Mohammad S Anwar + +You are given an integer, $n. + +Write a script to find an array containing $n unique integers such that they add up to zero. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +sub uniqSumZero ($n) { + my @n = (0); + if ($n > 1) { + @n = map { (-$_,$_) } 1 .. $n >> 1; + push(@n,0) if ($n % 2 != 0); + } + [sort { $a <=> $b } @n]; +} + +is(uniqSumZero(1),[0]); +is(uniqSumZero(2),[-1,1]); +is(uniqSumZero(3),[-1,0,1]); +is(uniqSumZero(4),[-2,-1,1,2]); +is(uniqSumZero(5),[-2,-1,0,1,2]); + +done_testing; |
