aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--challenge-246/paulo-custodio/Makefile2
-rw-r--r--challenge-246/paulo-custodio/perl/ch-1.pl36
-rw-r--r--challenge-246/paulo-custodio/t/test-1.yaml11
-rw-r--r--challenge-250/paulo-custodio/Makefile2
-rw-r--r--challenge-250/paulo-custodio/perl/ch-1.pl48
-rw-r--r--challenge-250/paulo-custodio/perl/ch-2.pl47
-rw-r--r--challenge-250/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-250/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-251/paulo-custodio/Makefile2
-rw-r--r--challenge-251/paulo-custodio/perl/ch-1.pl73
-rw-r--r--challenge-251/paulo-custodio/perl/ch-2.pl99
-rw-r--r--challenge-251/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-251/paulo-custodio/t/test-2.yaml15
-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
18 files changed, 499 insertions, 0 deletions
diff --git a/challenge-246/paulo-custodio/Makefile b/challenge-246/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-246/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-246/paulo-custodio/perl/ch-1.pl b/challenge-246/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..06ba5ed0b9
--- /dev/null
+++ b/challenge-246/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 246
+#
+# Task 1: 6 out of 49
+# Submitted by: Andreas Voegele
+#
+# 6 out of 49 is a German lottery.
+#
+# Write a script that outputs six unique random integers from the range 1 to 49.
+# Output
+#
+# 3
+# 10
+# 11
+# 22
+# 38
+# 49
+
+use Modern::Perl;
+
+use constant NR_BALLS => 49;
+use constant NR_DRAWS => 6;
+
+if (@ARGV) {
+ srand($ARGV[0]+0);
+}
+
+my @balls = (1..NR_BALLS);
+my @drawn;
+for (1..NR_DRAWS) {
+ my $i = int(rand()*scalar(@balls));
+ push @drawn, splice(@balls, $i, 1);
+}
+
+say join("\n", sort {$a<=>$b} @drawn);
diff --git a/challenge-246/paulo-custodio/t/test-1.yaml b/challenge-246/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..e0f8f2c284
--- /dev/null
+++ b/challenge-246/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,11 @@
+- setup:
+ cleanup:
+ args: 1234
+ input:
+ output: |
+ |11
+ |14
+ |16
+ |17
+ |37
+ |49
diff --git a/challenge-250/paulo-custodio/Makefile b/challenge-250/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-250/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-250/paulo-custodio/perl/ch-1.pl b/challenge-250/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..8667ad033c
--- /dev/null
+++ b/challenge-250/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+# Challenge 250
+#
+# Task 1: Smallest Index
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the smallest index i such that i mod 10 == $ints[i] otherwise return -1.
+# Example 1
+#
+# Input: @ints = (0, 1, 2)
+# Output: 0
+#
+# i=0: 0 mod 10 = 0 == $ints[0].
+# i=1: 1 mod 10 = 1 == $ints[1].
+# i=2: 2 mod 10 = 2 == $ints[2].
+# All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+#
+# Example 2
+#
+# Input: @ints = (4, 3, 2, 1)
+# Output: 2
+#
+# i=0: 0 mod 10 = 0 != $ints[0].
+# i=1: 1 mod 10 = 1 != $ints[1].
+# i=2: 2 mod 10 = 2 == $ints[2].
+# i=3: 3 mod 10 = 3 != $ints[3].
+# 2 is the only index which has i mod 10 == $ints[i].
+#
+# Example 3
+#
+# Input: @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
+# Output: -1
+# Explanation: No index satisfies i mod 10 == $ints[i].
+
+use Modern::Perl;
+
+say smallest_index(@ARGV);
+
+sub smallest_index {
+ my(@ints) = @_;
+ for my $i (0 .. $#ints) {
+ return $i if ($i % 10) == $ints[$i];
+ }
+ return -1;
+}
diff --git a/challenge-250/paulo-custodio/perl/ch-2.pl b/challenge-250/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..c0994d31d8
--- /dev/null
+++ b/challenge-250/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+# Challenge 250
+#
+# Task 2: Alphanumeric String Value
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of alphanumeric strings.
+#
+# Write a script to return the maximum value of alphanumeric string in the given array.
+#
+# The value of alphanumeric string can be defined as
+#
+# a) The numeric representation of the string in base 10 if it is made up of digits only.
+# b) otherwise the length of the string
+#
+#
+# Example 1
+#
+# Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+# Output: 6
+#
+# "perl" consists of letters only so the value is 4.
+# "2" is digits only so the value is 2.
+# "000" is digits only so the value is 0.
+# "python" consits of letters so the value is 6.
+# "r4ku" consists of letters and digits so the value is 4.
+#
+# Example 2
+#
+# Input: @alphanumstr = ("001", "1", "000", "0001")
+# Output: 1
+
+use Modern::Perl;
+use List::Util 'max';
+
+say max(map {str_value($_)} @ARGV);
+
+sub str_value {
+ my($str) = @_;
+ if ($str =~ /^\d+$/) {
+ return 0+$str;
+ }
+ else {
+ return length($str);
+ }
+}
diff --git a/challenge-250/paulo-custodio/t/test-1.yaml b/challenge-250/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..b769218e2d
--- /dev/null
+++ b/challenge-250/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 0 1 2
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 4 3 2 1
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 1 2 3 4 5 6 7 8 9 0
+ input:
+ output: -1
diff --git a/challenge-250/paulo-custodio/t/test-2.yaml b/challenge-250/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..0082789893
--- /dev/null
+++ b/challenge-250/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: perl 2 000 python r4ku
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 001 1 000 0001
+ input:
+ output: 1
diff --git a/challenge-251/paulo-custodio/Makefile b/challenge-251/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-251/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-251/paulo-custodio/perl/ch-1.pl b/challenge-251/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d61492ca82
--- /dev/null
+++ b/challenge-251/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+
+# Challenge 251
+#
+# Task 1: Concatenation Value
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the concatenation value of the given array.
+#
+# The concatenation of two numbers is the number formed by concatenating their numerals.
+#
+# For example, the concatenation of 10, 21 is 1021.
+# The concatenation value of @ints is initially equal to 0.
+# Perform this operation until @ints becomes empty:
+#
+# If there exists more than one number in @ints, pick the first element
+# and last element in @ints respectively and add the value of their
+# concatenation to the concatenation value of @ints, then delete the
+# first and last element from @ints.
+#
+# If one element exists, add its value to the concatenation value of
+# @ints, then delete it.
+#
+#
+# Example 1
+#
+# Input: @ints = (6, 12, 25, 1)
+# Output: 1286
+#
+# 1st operation: concatenation of 6 and 1 is 61
+# 2nd operation: concaternation of 12 and 25 is 1225
+#
+# Concatenation Value => 61 + 1225 => 1286
+#
+# Example 2
+#
+# Input: @ints = (10, 7, 31, 5, 2, 2)
+# Output: 489
+#
+# 1st operation: concatenation of 10 and 2 is 102
+# 2nd operation: concatenation of 7 and 2 is 72
+# 3rd operation: concatenation of 31 and 5 is 315
+#
+# Concatenation Value => 102 + 72 + 315 => 489
+#
+# Example 3
+#
+# Input: @ints = (1, 2, 10)
+# Output: 112
+#
+# 1st operation: concatenation of 1 and 10 is 110
+# 2nd operation: only element left is 2
+#
+# Concatenation Value => 110 + 2 => 112
+
+use Modern::Perl;
+
+my @ints = @ARGV;
+say sum_concat(@ints);
+
+sub sum_concat {
+ my(@ints) = @_;
+ my $sum = 0;
+ while (@ints > 1) {
+ my $n = $ints[0] . $ints[-1];
+ $sum += $n;
+ pop @ints; shift @ints;
+ }
+ $sum += $ints[0] if @ints;
+ return $sum;
+}
diff --git a/challenge-251/paulo-custodio/perl/ch-2.pl b/challenge-251/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..7d768473b0
--- /dev/null
+++ b/challenge-251/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,99 @@
+#!/usr/bin/env perl
+
+# Challenge 251
+#
+# Task 2: Lucky Numbers
+# Submitted by: Mohammad S Anwar
+#
+# You are given a m x n matrix of distinct numbers.
+#
+# Write a script to return the lucky number, if there is one, or -1 if not.
+#
+# A lucky number is an element of the matrix such that it is
+# the minimum element in its row and maximum in its column.
+#
+#
+# Example 1
+#
+# Input: $matrix = [ [ 3, 7, 8],
+# [ 9, 11, 13],
+# [15, 16, 17] ];
+# Output: 15
+#
+# 15 is the only lucky number since it is the minimum in its row
+# and the maximum in its column.
+#
+# Example 2
+#
+# Input: $matrix = [ [ 1, 10, 4, 2],
+# [ 9, 3, 8, 7],
+# [15, 16, 17, 12] ];
+# Output: 12
+#
+# Example 3
+#
+# Input: $matrix = [ [7 ,8],
+# [1 ,2] ];
+# Output: 7
+
+use Modern::Perl;
+
+my @matrix = parse_matrix("@ARGV");
+say lucky_number(@matrix);
+
+sub parse_matrix {
+ my($input) = @_;
+ my @matrix;
+ $input =~ s/^\s*\[\s*//s or die "missing starting [ in '$input'";
+ $input =~ s/\s*\]\s*$//s or die "missing ending ] in '$input'";
+ $input =~ s/^\s+//s;
+ while ($input ne '') {
+ ($input, my @row) = parse_row($input);
+ push @matrix, \@row;
+ $input =~ s/^\s*,?\s*//s;
+ }
+ return @matrix;
+}
+
+sub parse_row {
+ my($input) = @_;
+ $input =~ s/^\s*\[\s*//s or die "missing starting [ in '$input'";
+ (my $row, $input) = $input =~ /(.*?)\]\s*(.*)/s or die die "missing ending ] in '$input'";
+ my @row = split(/(?:\s|,)+/, $row);
+ return ($input, @row);
+}
+
+sub lucky_number {
+ my(@matrix) = @_;
+ for my $r (0 .. $#matrix) {
+ my $row = $matrix[$r];
+ my $c = min_col_in_row(@$row);
+ my $max_r = max_row_in_col($c, @matrix);
+ if ($r == $max_r) {
+ return $matrix[$r][$c];
+ }
+ }
+ return -1;
+}
+
+sub min_col_in_row {
+ my(@row) = @_;
+ my $min_c = 0;
+ for my $c (0 .. $#row) {
+ if ($row[$c] < $row[$min_c]) {
+ $min_c = $c;
+ }
+ }
+ return $min_c;
+}
+
+sub max_row_in_col {
+ my($c, @matrix) = @_;
+ my $max_r = 0;
+ for my $r (0 .. $#matrix) {
+ if ($matrix[$r][$c] > $matrix[$max_r][$c]) {
+ $max_r = $r;
+ }
+ }
+ return $max_r;
+}
diff --git a/challenge-251/paulo-custodio/t/test-1.yaml b/challenge-251/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..77e3ea9def
--- /dev/null
+++ b/challenge-251/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 6 12 25 1
+ input:
+ output: 1286
+- setup:
+ cleanup:
+ args: 10 7 31 5 2 2
+ input:
+ output: 489
+- setup:
+ cleanup:
+ args: 1 2 10
+ input:
+ output: 112
diff --git a/challenge-251/paulo-custodio/t/test-2.yaml b/challenge-251/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..a30d74bcc9
--- /dev/null
+++ b/challenge-251/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: "'[ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]'"
+ input:
+ output: 15
+- setup:
+ cleanup:
+ args: "'[ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]'"
+ input:
+ output: 12
+- setup:
+ cleanup:
+ args: "'[ [7 ,8], [1 ,2] ]'"
+ input:
+ output: 7
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)