aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-10 09:17:26 +0100
committerGitHub <noreply@github.com>2025-06-10 09:17:26 +0100
commitd2b2ac2b96a3df5dbe8dc317cf9e8c430688500d (patch)
treedd6e02a9926b6cd08ac143c30da7b86b1de65094
parente28477d2418099cfbb1a227133c69ddef6eed741 (diff)
parentdea6d7c776270202135eeb2e85903b5a4814473f (diff)
downloadperlweeklychallenge-club-d2b2ac2b96a3df5dbe8dc317cf9e8c430688500d.tar.gz
perlweeklychallenge-club-d2b2ac2b96a3df5dbe8dc317cf9e8c430688500d.tar.bz2
perlweeklychallenge-club-d2b2ac2b96a3df5dbe8dc317cf9e8c430688500d.zip
Merge pull request #12150 from jeanluc2020/jeanluc2020-325
Add solution 325.
-rw-r--r--challenge-325/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-325/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-325/jeanluc2020/perl/ch-1.pl63
-rwxr-xr-xchallenge-325/jeanluc2020/perl/ch-2.pl114
4 files changed, 179 insertions, 0 deletions
diff --git a/challenge-325/jeanluc2020/blog-1.txt b/challenge-325/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..d588c8b7bd
--- /dev/null
+++ b/challenge-325/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-325-1.html
diff --git a/challenge-325/jeanluc2020/blog-2.txt b/challenge-325/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..3c0f21d1f5
--- /dev/null
+++ b/challenge-325/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-325-2.html
diff --git a/challenge-325/jeanluc2020/perl/ch-1.pl b/challenge-325/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..da5268c40c
--- /dev/null
+++ b/challenge-325/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-325/#TASK1
+#
+# Task 1: Consecutive One
+# =======================
+#
+# You are given a binary array containing only 0 or/and 1.
+#
+# Write a script to find out the maximum consecutive 1 in the given array.
+#
+## Example 1
+##
+## Input: @binary = (0, 1, 1, 0, 1, 1, 1)
+## Output: 3
+#
+#
+## Example 2
+##
+## Input: @binary = (0, 0, 0, 0)
+## Output: 0
+#
+#
+## Example 3
+##
+## Input: @binary = (1, 0, 1, 0, 1, 1)
+## Output: 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We need to keep track of the current consecutive number of 1s and
+# of the longest streak so far in two variables. For each element
+# we need to check whether it's a one or zero. In the latter case, we
+# reset $current to 0, otherwise we add one to $current and if $current
+# is bigger than the longest streak, we set that variable as well.
+#
+
+use v5.36;
+
+consecutive_one(0, 1, 1, 0, 1, 1, 1);
+consecutive_one(0, 0, 0, 0);
+consecutive_one(1, 0, 1, 0, 1, 1);
+
+sub consecutive_one( @binary ) {
+ say "binary = (" . join(", ", @binary) . ")";
+ my $longest = 0;
+ my $current = 0;
+ foreach my $elem (@binary) {
+ if($elem) {
+ $current++;
+ if($current > $longest) {
+ $longest = $current;
+ }
+ } else {
+ $current = 0;
+ }
+ }
+ say "Output: $longest";
+}
+
diff --git a/challenge-325/jeanluc2020/perl/ch-2.pl b/challenge-325/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..3e1dbb723a
--- /dev/null
+++ b/challenge-325/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,114 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-325/#TASK2
+#
+# Task 2: Final Price
+# ===================
+#
+# You are given an array of item prices.
+#
+# Write a script to find out the final price of each items in the given array.
+#
+# There is a special discount scheme going on. If there’s an item with a lower
+# or equal price later in the list, you get a discount equal to that later
+# price (the first one you find in order).
+#
+## Example 1
+##
+## Input: @prices = (8, 4, 6, 2, 3)
+## Output: (4, 2, 4, 2, 3)
+##
+## Item 0:
+## The item price is 8.
+## The first time that has price <= current item price is 4.
+## Final price = 8 - 4 => 4
+##
+## Item 1:
+## The item price is 4.
+## The first time that has price <= current item price is 2.
+## Final price = 4 - 2 => 2
+##
+## Item 2:
+## The item price is 6.
+## The first time that has price <= current item price is 2.
+## Final price = 6 - 2 => 4
+##
+## Item 3:
+## The item price is 2.
+## No item has price <= current item price, no discount.
+## Final price = 2
+##
+## Item 4:
+## The item price is 3.
+## Since it is the last item, so no discount.
+## Final price = 3
+#
+#
+## Example 2
+##
+## Input: @prices = (1, 2, 3, 4, 5)
+## Output: (1, 2, 3, 4, 5)
+#
+#
+## Example 3
+##
+## Input: @prices = (7, 1, 1, 5)
+## Output: (6, 0, 1, 5)
+##
+## Item 0:
+## The item price is 7.
+## The first time that has price <= current item price is 1.
+## Final price = 7 - 1 => 6
+##
+## Item 1:
+## The item price is 1.
+## The first time that has price <= current item price is 1.
+## Final price = 1 - 1 => 0
+##
+## Item 2:
+## The item price is 1.
+## No item has price <= current item price, so no discount.
+## Final price = 1
+##
+## Item 3:
+## The item price is 5.
+## Since it is the last item, so no discount.
+## Final price = 5
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We walk from the beginning of @prices to its end. In so doing,
+# we check all elements from that element to the end to see if one
+# is less than or equal in price. If yes, we calculate the new
+# discounted price and stop walking to the end for this iteration.
+# Then we check whether there is a discounted price and use either
+# that or the original price if there isn't.
+
+use v5.36;
+
+final_price(8, 4, 6, 2, 3);
+final_price(1, 2, 3, 4, 5);
+final_price(7, 1, 1, 5);
+
+sub final_price( @prices ) {
+ say "Input: (" . join(", ", @prices) . ")";
+ my @result = ();
+ foreach my $i (0..$#prices) {
+ my $discounted = -1;
+ foreach my $j ($i+1..$#prices) {
+ if($prices[$j] <= $prices[$i]) {
+ $discounted = $prices[$i] - $prices[$j];
+ last;
+ }
+ }
+ if($discounted >= 0) {
+ push @result, $discounted;
+ } else {
+ push @result, $prices[$i];
+ }
+ }
+ say "Output: (" . join(", ", @result) . ")";
+}