aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-16 18:24:58 +0000
committerGitHub <noreply@github.com>2024-01-16 18:24:58 +0000
commit0a58f99b986847aedfc01897a31e8e9e8f6f12d3 (patch)
tree7548c80495add0e9c7624cb19978686826897b2b
parent9d38db293e8a5fc5bc5958d1d26d662ae354f8af (diff)
parenta200f84e96cb99ec38b7d0cc2e8db325f25842fa (diff)
downloadperlweeklychallenge-club-0a58f99b986847aedfc01897a31e8e9e8f6f12d3.tar.gz
perlweeklychallenge-club-0a58f99b986847aedfc01897a31e8e9e8f6f12d3.tar.bz2
perlweeklychallenge-club-0a58f99b986847aedfc01897a31e8e9e8f6f12d3.zip
Merge pull request #9404 from PerlBoy1967/branch-for-challenge-252
w252 - Task 1 & 2
-rwxr-xr-xchallenge-252/perlboy1967/perl/ch1.pl38
-rwxr-xr-xchallenge-252/perlboy1967/perl/ch2.pl40
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;