aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-01-15 18:31:07 +0100
committerpme <hauptadler@gmail.com>2024-01-15 18:31:07 +0100
commit1db3afdd13bf352c2ebbb1b9ebbc2d74b5954ad1 (patch)
tree1984f66fb54c8586e845352896a76f224b335956 /challenge-252
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
downloadperlweeklychallenge-club-1db3afdd13bf352c2ebbb1b9ebbc2d74b5954ad1.tar.gz
perlweeklychallenge-club-1db3afdd13bf352c2ebbb1b9ebbc2d74b5954ad1.tar.bz2
perlweeklychallenge-club-1db3afdd13bf352c2ebbb1b9ebbc2d74b5954ad1.zip
challenge-252
Diffstat (limited to 'challenge-252')
-rwxr-xr-xchallenge-252/peter-meszaros/perl/ch-1.pl66
-rwxr-xr-xchallenge-252/peter-meszaros/perl/ch-2.pl58
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-252/peter-meszaros/perl/ch-1.pl b/challenge-252/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..f13598dec3
--- /dev/null
+++ b/challenge-252/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+#
+# 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 strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [1, 2, 3, 4],
+ [2, 7, 1, 19, 18, 3],
+];
+
+sub special_numbers
+{
+ my $l = shift;
+
+ my $len = @$l;
+ my $sum = 0;
+ for my $i (1..$len) {
+ unless ($len % $i) {
+ $sum += $l->[$i-1]**2;
+ }
+ }
+ return $sum;
+}
+
+is(special_numbers($cases->[0]), 21, 'Example 1');
+is(special_numbers($cases->[1]), 63, 'Example 2');
+done_testing();
+
+exit 0;
diff --git a/challenge-252/peter-meszaros/perl/ch-2.pl b/challenge-252/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..d3db6ad3ec
--- /dev/null
+++ b/challenge-252/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+#
+# 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 strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ 5,
+ 3,
+ 1,
+ 6,
+];
+
+sub unique_sum_zero
+{
+ my $n = shift;
+
+ my $arr = [];
+ for my $i (1..($n/2)) {
+ push @$arr, $i, -$i;
+ }
+ push @$arr, 0 if $n % 2;
+
+ return $arr;
+}
+
+is(unique_sum_zero($cases->[0]), [1, -1, 2, -2, 0], 'Example 1');
+is(unique_sum_zero($cases->[1]), [1, -1, 0], 'Example 2');
+is(unique_sum_zero($cases->[2]), [0], 'Example 3');
+is(unique_sum_zero($cases->[3]), [1, -1, 2, -2, 3, -3], 'Example 4');
+done_testing();
+
+exit 0;
+
+