aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-01-15 18:08:48 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2024-01-15 18:08:48 +0000
commitdd84ff9b392f33fa468e62ada780f8713e9fbccb (patch)
treecaaefdf4b62da17489336fb21641340d6f782b93
parent125b34195ca97f5e8265837b368a35c7a0f1327f (diff)
downloadperlweeklychallenge-club-dd84ff9b392f33fa468e62ada780f8713e9fbccb.tar.gz
perlweeklychallenge-club-dd84ff9b392f33fa468e62ada780f8713e9fbccb.tar.bz2
perlweeklychallenge-club-dd84ff9b392f33fa468e62ada780f8713e9fbccb.zip
Add Perl solution
-rw-r--r--challenge-252/paulo-custodio/Makefile2
-rw-r--r--challenge-252/paulo-custodio/README1
-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
6 files changed, 125 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/README b/challenge-252/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-252/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
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)