aboutsummaryrefslogtreecommitdiff
path: root/challenge-235/wanderdoc/perl
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-09-24 18:36:52 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-09-24 18:36:52 +0100
commitbdec35717971a5e60227cb25205e52ece8200204 (patch)
tree3d0ded1f45d598653882b51b0ed478e998ed7b5c /challenge-235/wanderdoc/perl
parenta14b80286efaa1c8fec293dae7b5b94d702d1323 (diff)
downloadperlweeklychallenge-club-bdec35717971a5e60227cb25205e52ece8200204.tar.gz
perlweeklychallenge-club-bdec35717971a5e60227cb25205e52ece8200204.tar.bz2
perlweeklychallenge-club-bdec35717971a5e60227cb25205e52ece8200204.zip
- Added solutions by Stephen G Lynn.
- Added solutions by Jaldhar H. Vyas. - Added solutions by Peter Campbell Smith. - Added solutions by Arne Sommer. - Added solutions by Avery Adams. - Added solutions by Jorg Sommrey. - Added solutions by Roger Bell_West. - Added solutions by Cheok-Yin Fung. - Added solutions by Solathian. - Added solutions by Simon Green. - Added solutions by Jan Krnavek. - Added solutions by Bob Lied. - Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-235/wanderdoc/perl')
-rwxr-xr-xchallenge-235/wanderdoc/perl/ch-1.pl38
-rwxr-xr-xchallenge-235/wanderdoc/perl/ch-2.pl45
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-235/wanderdoc/perl/ch-1.pl b/challenge-235/wanderdoc/perl/ch-1.pl
new file mode 100755
index 0000000000..d2bcb0e13b
--- /dev/null
+++ b/challenge-235/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an array of integers. Write a script to find out if removing ONLY one integer makes it strictly increasing order.
+Example 1 Input: @ints = (0, 2, 9, 4, 6) Output: true
+Removing ONLY 9 in the given array makes it strictly increasing order.
+Example 2 Input: @ints = (5, 1, 3, 2) Output: false
+Example 3 Input: @ints = (2, 2, 3) Output: true
+=cut
+
+
+
+use List::Util qw(uniq);
+use Scalar::Util qw(dualvar);
+use Test2::V0;
+
+
+sub remove_one
+{
+ my @arr = @_;
+ my @idx = 0 .. $#arr;
+ for my $i ( @idx )
+ {
+ my @cand = @arr[grep {$_ != $i} @idx];
+ if ( join(" ", @cand) eq join(" ", sort {$a <=> $b } uniq @cand ) )
+ {
+ return dualvar( $arr[$i], 'true' );
+ }
+ }
+ return 'false';
+}
+
+is(remove_one(0, 2, 9, 4, 6), 'true', 'Example 1');
+is(remove_one(5, 1, 3, 2), 'false', 'Example 2');
+is(remove_one(2, 2, 3), 'true', 'Example 3');
+done_testing(); \ No newline at end of file
diff --git a/challenge-235/wanderdoc/perl/ch-2.pl b/challenge-235/wanderdoc/perl/ch-2.pl
new file mode 100755
index 0000000000..73a5bcaf92
--- /dev/null
+++ b/challenge-235/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!perl
+use strict;
+use warnings FATAL=> qw(all);
+
+=prompt
+You are given an array of integers. Write a script to duplicate each occurrence of ZERO in the given array and shift the remaining to the right but make sure the size of array remain the same.
+Example 1 Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) Output: (1, 0, 0, 2, 3, 0, 0, 4)
+Example 2 Input: @ints = (1, 2, 3) Output: (1, 2, 3)
+Example 3 Input: @ints = (0, 3, 0, 4, 5) Output: (0, 0, 3, 0, 0)
+=cut
+
+
+
+
+
+
+use Test2::V0;
+
+sub duplicate_zeros
+{
+ my @arr = @_;
+ my $max_idx = $#arr;
+ my @output;
+ for my $i ( 0 .. $max_idx )
+ {
+ push @output, $arr[$i];
+ if ( $arr[$i] == 0 )
+ {
+ push @output, 0;
+ }
+ if ( $#output > $max_idx )
+ {
+ return @output[0 .. $max_idx];
+ }
+ }
+ return @output;
+}
+
+
+
+is([duplicate_zeros(1, 0, 2, 3, 0, 4, 5, 0)], [(1, 0, 0, 2, 3, 0, 0, 4)], 'Example 1');
+is([duplicate_zeros(1, 2, 3)], [(1, 2, 3)], 'Example 2');
+is([duplicate_zeros(0, 3, 0, 4, 5)], [(0, 0, 3, 0, 0)], 'Example 3');
+done_testing();
+