aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-08-18 17:49:37 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-08-18 17:49:37 +0100
commit7a6f61b8cb38ea32ab526a93798f7b515f9233b4 (patch)
treebd8ada644885f175b3ec542a89ecbc9a96fea357
parentffcaa428637c38e3716817bf61e9c9c4c7c97cd8 (diff)
downloadperlweeklychallenge-club-7a6f61b8cb38ea32ab526a93798f7b515f9233b4.tar.gz
perlweeklychallenge-club-7a6f61b8cb38ea32ab526a93798f7b515f9233b4.tar.bz2
perlweeklychallenge-club-7a6f61b8cb38ea32ab526a93798f7b515f9233b4.zip
Add Perl solution to challenge 226
-rw-r--r--challenge-226/paulo-custodio/Makefile2
-rw-r--r--challenge-226/paulo-custodio/perl/ch-1.pl22
-rw-r--r--challenge-226/paulo-custodio/perl/ch-2.pl50
-rw-r--r--challenge-226/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-226/paulo-custodio/t/test-2.yaml15
5 files changed, 99 insertions, 0 deletions
diff --git a/challenge-226/paulo-custodio/Makefile b/challenge-226/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-226/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-226/paulo-custodio/perl/ch-1.pl b/challenge-226/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..9e3887ae3b
--- /dev/null
+++ b/challenge-226/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+
+# Challenge 226
+#
+# Task 1: Shuffle String
+# Submitted by: Mohammad S Anwar
+# You are given a string and an array of indices of same length as string.
+#
+# Write a script to return the string after re-arranging the indices in the correct order.
+#
+# Example 1
+# Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1)
+# Output: 'challenge'
+# Example 2
+# Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6)
+# Output: 'perlraku'
+
+use Modern::Perl;
+
+my($str, @idx) = @ARGV;
+my @chars = split //, $str;
+say join('', map {$_->[1]} sort {$a->[0] <=> $b->[0]} map {[$idx[$_], $chars[$_]]} 0..$#chars);
diff --git a/challenge-226/paulo-custodio/perl/ch-2.pl b/challenge-226/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..1effcf0822
--- /dev/null
+++ b/challenge-226/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+# Challenge 226
+#
+# Task 2: Zero Array
+# Submitted by: Mohammad S Anwar
+# You are given an array of non-negative integers, @ints.
+#
+# Write a script to return the minimum number of operations to make every
+# element equal zero.
+#
+# In each operation, you are required to pick a positive number less than or
+# equal to the smallest element in the array, then subtract that from each
+# positive element in the array.
+#
+#
+# Example 1:
+# Input: @ints = (1, 5, 0, 3, 5)
+# Output: 3
+#
+# operation 1: pick 1 => (0, 4, 0, 2, 4)
+# operation 2: pick 2 => (0, 2, 0, 0, 2)
+# operation 3: pick 2 => (0, 0, 0, 0, 0)
+# Example 2:
+# Input: @ints = (0)
+# Output: 0
+# Example 3:
+# Input: @ints = (2, 1, 4, 0, 3)
+# Output: 4
+#
+# operation 1: pick 1 => (1, 0, 3, 0, 2)
+# operation 2: pick 1 => (0, 0, 2, 0, 1)
+# operation 3: pick 1 => (0, 0, 1, 0, 0)
+# operation 4: pick 1 => (0, 0, 0, 0, 0)
+
+use Modern::Perl;
+use List::Util 'min';
+
+say num_ops(@ARGV);
+
+sub num_ops {
+ my(@n) = @_;
+ my $ops = 0;
+ while (grep {$_ > 0} @n) {
+ my $min = min(grep {$_ > 0} @n);
+ @n = map {$_ > 0 ? $_-$min : 0} @n;
+ $ops++;
+ }
+ return $ops;
+}
diff --git a/challenge-226/paulo-custodio/t/test-1.yaml b/challenge-226/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..94a37a6263
--- /dev/null
+++ b/challenge-226/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: lacelengh 3 2 0 5 4 8 6 7 1
+ input:
+ output: challenge
+- setup:
+ cleanup:
+ args: rulepark 4 7 3 1 0 5 2 6
+ input:
+ output: perlraku
diff --git a/challenge-226/paulo-custodio/t/test-2.yaml b/challenge-226/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5a8ebeb4fe
--- /dev/null
+++ b/challenge-226/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1 5 0 3 5
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 2 1 4 0 3
+ input:
+ output: 4