aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-06-16 21:34:01 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-06-16 21:34:01 +0200
commit7dc1f42a6ed2046803d05f52eb9d765ae63daf34 (patch)
treeaf61876d77e3972160084cca274df79ccb1d8349
parent26cfae99bb0a2fdf9710bcc51e8abc8d7ed627f6 (diff)
downloadperlweeklychallenge-club-7dc1f42a6ed2046803d05f52eb9d765ae63daf34.tar.gz
perlweeklychallenge-club-7dc1f42a6ed2046803d05f52eb9d765ae63daf34.tar.bz2
perlweeklychallenge-club-7dc1f42a6ed2046803d05f52eb9d765ae63daf34.zip
Add solution 326.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-326/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-326/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-326/jeanluc2020/perl/ch-1.pl75
-rwxr-xr-xchallenge-326/jeanluc2020/perl/ch-2.pl62
4 files changed, 139 insertions, 0 deletions
diff --git a/challenge-326/jeanluc2020/blog-1.txt b/challenge-326/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..d5858ef483
--- /dev/null
+++ b/challenge-326/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-326-1.html
diff --git a/challenge-326/jeanluc2020/blog-2.txt b/challenge-326/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..80d8bcba00
--- /dev/null
+++ b/challenge-326/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-326-2.html
diff --git a/challenge-326/jeanluc2020/perl/ch-1.pl b/challenge-326/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..a4a39c597d
--- /dev/null
+++ b/challenge-326/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-326/#TASK1
+#
+# Task 1: Day of the Year
+# =======================
+#
+# You are given a date in the format YYYY-MM-DD.
+#
+# Write a script to find day number of the year that the given date represent.
+#
+## Example 1
+##
+## Input: $date = '2025-02-02'
+## Output: 33
+##
+## The 2nd Feb, 2025 is the 33rd day of the year.
+#
+#
+## Example 2
+##
+## Input: $date = '2025-04-10'
+## Output: 100
+#
+#
+## Example 3
+##
+## Input: $date = '2025-09-07'
+## Output: 250
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We need to add up all days in the months up to before the current one.
+# Then we need to add the day in the current month to that sum.
+# We just need one little trick: An array with the number of days for
+# each number so we can add those up easily. This in turn needs to check
+# whether we're in a leap year so we can set February to 29 days instead
+# of the usual 28.
+
+use v5.36;
+
+day_of_year('2025-02-02');
+day_of_year('2025-04-10');
+day_of_year('2025-09-07');
+
+sub day_of_year($date) {
+ say "Input: $date";
+ my ($y, $m, $d) = split /-/, $date;
+ $m =~ s/^0//;
+ $d =~ s/^0//;
+ my @mdays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+ $mdays[1] = 29 if is_leap_year($y);
+ my $s = 0;
+ foreach my $i (0..$m-2) {
+ $s += $mdays[$i];
+ }
+ $s += $d;
+ say "Output: $s";
+}
+
+sub is_leap_year($year) {
+ if($year % 4) {
+ return 0;
+ }
+ if($year % 100 == 0) {
+ if($year % 400 == 0) {
+ return 1;
+ }
+ return 0;
+ }
+ return 1;
+}
diff --git a/challenge-326/jeanluc2020/perl/ch-2.pl b/challenge-326/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..90f6438b1b
--- /dev/null
+++ b/challenge-326/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-326/#TASK2
+#
+# Task 2: Decompressed List
+# =========================
+#
+# You are given an array of positive integers having even elements.
+#
+# Write a script to to return the decompress list. To decompress, pick adjacent
+# pair (i, j) and replace it with j, i times.
+#
+## Example 1
+##
+## Input: @ints = (1, 3, 2, 4)
+## Output: (3, 4, 4)
+##
+## Pair 1: (1, 3) => 3 one time => (3)
+## Pair 2: (2, 4) => 4 two times => (4, 4)
+#
+#
+## Example 2
+##
+## Input: @ints = (1, 1, 2, 2)
+## Output: (1, 2, 2)
+##
+## Pair 1: (1, 1) => 1 one time => (1)
+## Pair 2: (2, 2) => 2 two times => (2, 2)
+#
+#
+## Example 3
+##
+## Input: @ints = (3, 1, 3, 2)
+## Output: (1, 1, 1, 2, 2, 2)
+##
+## Pair 1: (3, 1) => 1 three times => (1, 1, 1)
+## Pair 2: (3, 2) => 2 three times => (2, 2, 2)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Since perl v5.36 has the possibility to run foreach with two elements
+# of a list at once, we use that neat feature to walk all pairs of
+# $i and $j. Then we put $j into the result $i times.
+
+use v5.36;
+decompressed_list(1, 3, 2, 4);
+decompressed_list(1, 1, 2, 2);
+decompressed_list(3, 1, 3, 2);
+
+sub decompressed_list( @ints ) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my @output = ();
+ foreach my ($i, $j) (@ints) {
+ foreach my $k (1..$i) {
+ push @output, $j;
+ }
+ }
+ say "Output: (" . join(", ", @output) . ")";
+}