aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-01-16 14:00:52 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2024-01-16 14:00:52 +0000
commit20301d432f4af188abbe48fbfab9e0eb93acfb76 (patch)
tree8666bd8d8681629e13d704d5706416f89720f505
parent722a465192340594398a547cd26740855a88f805 (diff)
downloadperlweeklychallenge-club-20301d432f4af188abbe48fbfab9e0eb93acfb76.tar.gz
perlweeklychallenge-club-20301d432f4af188abbe48fbfab9e0eb93acfb76.tar.bz2
perlweeklychallenge-club-20301d432f4af188abbe48fbfab9e0eb93acfb76.zip
Add Perl solution
-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
5 files changed, 122 insertions, 0 deletions
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