aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-16 20:07:17 +0000
committerGitHub <noreply@github.com>2024-01-16 20:07:17 +0000
commit47becd4d92af268e02cd61bb2b77816a4c8ed06b (patch)
tree7ef04a16c56da7ea74d19117364d2017208b378d /challenge-252
parent8512e57085695a3ebec76f51d65f217e0dbf0077 (diff)
parent20301d432f4af188abbe48fbfab9e0eb93acfb76 (diff)
downloadperlweeklychallenge-club-47becd4d92af268e02cd61bb2b77816a4c8ed06b.tar.gz
perlweeklychallenge-club-47becd4d92af268e02cd61bb2b77816a4c8ed06b.tar.bz2
perlweeklychallenge-club-47becd4d92af268e02cd61bb2b77816a4c8ed06b.zip
Merge pull request #9411 from pauloscustodio/master
Add Perl solution
Diffstat (limited to 'challenge-252')
-rw-r--r--challenge-252/paulo-custodio/Makefile2
-rw-r--r--challenge-252/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-252/paulo-custodio/perl/ch-2.pl36
-rw-r--r--challenge-252/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-252/paulo-custodio/t/test-2.yaml20
5 files changed, 124 insertions, 0 deletions
diff --git a/challenge-252/paulo-custodio/Makefile b/challenge-252/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-252/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-252/paulo-custodio/perl/ch-1.pl b/challenge-252/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..b662b32b0d
--- /dev/null
+++ b/challenge-252/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+# Challenge 252
+#
+# 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.
+#
+#
+# Example 1
+#
+# Input: @ints = (1, 2, 3, 4)
+# Output: 21
+#
+# There are exactly 3 special elements in the given array:
+# $ints[1] since 1 divides 4,
+# $ints[2] since 2 divides 4, and
+# $ints[4] since 4 divides 4.
+#
+# Hence, the sum of the squares of all special elements of given array:
+# 1 * 1 + 2 * 2 + 4 * 4 = 21.
+#
+# Example 2
+#
+# Input: @ints = (2, 7, 1, 19, 18, 3)
+# Output: 63
+#
+# There are exactly 4 special elements in the given array:
+# $ints[1] since 1 divides 6,
+# $ints[2] since 2 divides 6,
+# $ints[3] since 3 divides 6, and
+# $ints[6] since 6 divides 6.
+#
+# Hence, the sum of the squares of all special elements of given array:
+# 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63
+
+use Modern::Perl;
+
+sub sum_squares_special_nums {
+ my(@nums) = @_;
+ my $sum = 0;
+ for my $i (1 .. @nums) {
+ if (scalar(@nums) % $i == 0) {
+ $sum += $nums[$i-1] * $nums[$i-1];
+ }
+ }
+ return $sum;
+}
+
+say sum_squares_special_nums(@ARGV);
diff --git a/challenge-252/paulo-custodio/perl/ch-2.pl b/challenge-252/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..9b814a359c
--- /dev/null
+++ b/challenge-252/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 252
+#
+# 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.
+# Example 1
+#
+# Input: $n = 5
+# Output: (-7, -1, 1, 3, 4)
+#
+# Two other possible solutions could be as below:
+# (-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).
+#
+# Example 2
+#
+# Input: $n = 3
+# Output: (-1, 0, 1)
+#
+# Example 3
+#
+# Input: $n = 1
+# Output: (0)
+
+use Modern::Perl;
+
+my $n = shift || 0;
+die "Usage: ch-2.pl N\n" if $n < 1;
+
+my @half = 1..int($n/2);
+my @out = ((reverse map {-$_} @half), (($n % 2 == 1) ? (0) : ()), (@half));
+say "(", join(", ", @out), ")";
diff --git a/challenge-252/paulo-custodio/t/test-1.yaml b/challenge-252/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..0a2ca1d066
--- /dev/null
+++ b/challenge-252/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1 2 3 4
+ input:
+ output: 21
+- setup:
+ cleanup:
+ args: 2 7 1 19 18 3
+ input:
+ output: 63
diff --git a/challenge-252/paulo-custodio/t/test-2.yaml b/challenge-252/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..66dca33dc7
--- /dev/null
+++ b/challenge-252/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: (0)
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: (-1, 1)
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: (-1, 0, 1)
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: (-2, -1, 1, 2)