aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-20 14:29:02 +0100
committerGitHub <noreply@github.com>2023-09-20 14:29:02 +0100
commit87b16e1b06cc524aedba65808fce2d17e2333934 (patch)
tree032b3d118b4b9b6f1217c4c6748dd692d32dd6fd
parent38dfe5470cbdf5df17b0a200d78093e5c3e42213 (diff)
parent0327045728ed85b7b7555df1c2531d2e255cfdb5 (diff)
downloadperlweeklychallenge-club-87b16e1b06cc524aedba65808fce2d17e2333934.tar.gz
perlweeklychallenge-club-87b16e1b06cc524aedba65808fce2d17e2333934.tar.bz2
perlweeklychallenge-club-87b16e1b06cc524aedba65808fce2d17e2333934.zip
Merge pull request #8730 from jeanluc2020/jeanluc-235
Add solution 235.
-rw-r--r--challenge-235/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-235/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-235/jeanluc2020/perl/ch-1.pl71
-rwxr-xr-xchallenge-235/jeanluc2020/perl/ch-2.pl70
4 files changed, 143 insertions, 0 deletions
diff --git a/challenge-235/jeanluc2020/blog-1.txt b/challenge-235/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..0d544ea373
--- /dev/null
+++ b/challenge-235/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-235-1.html
diff --git a/challenge-235/jeanluc2020/blog-2.txt b/challenge-235/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..a1a515f6ff
--- /dev/null
+++ b/challenge-235/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-235-2.html
diff --git a/challenge-235/jeanluc2020/perl/ch-1.pl b/challenge-235/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..02e1dee578
--- /dev/null
+++ b/challenge-235/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-235/#TASK1
+#
+# Task 1: Remove One
+# ==================
+#
+# 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
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's just remember that strictly increasing order means
+# each element is bigger than the previous one and can't even
+# be equal. So let's try to remove each element in turn and
+# check if the remaining array is strictly increasing.
+
+use strict;
+use warnings;
+
+remove_one(0, 2, 9, 4, 6);
+remove_one(5, 1, 3, 2);
+remove_one(2, 2, 3);
+
+sub remove_one {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ foreach my $index (0..$#ints) {
+ # if the remaining array with the element at the current index removed
+ # is strictly increasing then we found a solution that works
+ if(is_strictly_increasing(@ints[0..$index-1], @ints[$index+1..$#ints])) {
+ print "Output: True\n";
+ return;
+ }
+ }
+ print "Output: False\n";
+}
+
+sub is_strictly_increasing {
+ my @ints = @_;
+ my $previous = shift @ints;
+ # if any element is smaller or equal than the previous one, this
+ # array is not strictly increasing
+ foreach my $elem (@ints) {
+ return 0 if $elem <= $previous;
+ $previous = $elem;
+ }
+ return 1;
+}
diff --git a/challenge-235/jeanluc2020/perl/ch-2.pl b/challenge-235/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..91c7090c4e
--- /dev/null
+++ b/challenge-235/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-235/#TASK2
+#
+# Task 2: Duplicate Zeros
+# =======================
+#
+# 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)
+## Ouput: (1, 0, 0, 2, 3, 0, 0, 4)
+#
+## Example 2
+##
+## Input: @ints = (1, 2, 3)
+## Ouput: (1, 2, 3)
+#
+## Example 3
+##
+## Input: @ints = (0, 3, 0, 4, 5)
+## Ouput: (0, 0, 3, 0, 0)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's start by noting that there might be a situation where the last element
+# in the array is a 0 which can no longer be duplicated without changing the size
+# of the array, so we won't duplicate in that case.
+
+use strict;
+use warnings;
+
+duplicate_zeros(1, 0, 2, 3, 0, 4, 5, 0);
+duplicate_zeros(1, 2, 3);
+duplicate_zeros(0, 3, 0, 4, 5);
+duplicate_zeros(0, 3, 0, 4);
+
+sub duplicate_zeros {
+ my @ints = @_;
+ my @result = ();
+ print "Input: (" . join(", ", @ints) . ")\n";
+ while(@ints) {
+ my $first = shift @ints;
+ if(@ints) { # there are more elements in the array
+ if($first == 0) {
+ # duplicate the zero, remove last element from remainder
+ # this will effectively "shift right" the remainder of the
+ # array while keeping size
+ pop(@ints);
+ push @result, $first, 0;
+ } else {
+ # just move the non-zero value to the result
+ push @result, $first;
+ }
+ } else { # no more elements in the array
+ # this was the last element, so duplicating makes no sense any more
+ # just add the element to the result in either case
+ push @result, $first;
+ }
+ }
+ print "Output: (" . join(", ", @result) . ")\n";
+}