aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-06-17 11:25:27 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-06-17 11:25:27 +0100
commita791b4ffbc78b798ff7f0486dceca79c7810a289 (patch)
treee5855761ae5ea617c94db4d5b7adf0187b984bf5
parent93412c7fa19e013c244e41e417def795f7aac313 (diff)
downloadperlweeklychallenge-club-a791b4ffbc78b798ff7f0486dceca79c7810a289.tar.gz
perlweeklychallenge-club-a791b4ffbc78b798ff7f0486dceca79c7810a289.tar.bz2
perlweeklychallenge-club-a791b4ffbc78b798ff7f0486dceca79c7810a289.zip
Week 326 - Days to decompress
-rw-r--r--challenge-326/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-326/peter-campbell-smith/perl/ch-1.pl48
-rwxr-xr-xchallenge-326/peter-campbell-smith/perl/ch-2.pl35
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-326/peter-campbell-smith/blog.txt b/challenge-326/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..d4c00577e7
--- /dev/null
+++ b/challenge-326/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/326
diff --git a/challenge-326/peter-campbell-smith/perl/ch-1.pl b/challenge-326/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..5d3e8c976f
--- /dev/null
+++ b/challenge-326/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-16
+use utf8; # Week 326 - task 1 - Day of the year
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+day_of_the_year('2025-02-02');
+day_of_the_year('2025-04-10');
+day_of_the_year('2025-09-07');
+day_of_the_year('1900-12-31'); # not a leap year
+day_of_the_year('2000-12-31'); # was a leap year
+day_of_the_year('1819-05-24'); # Queeen Victoria born
+day_of_the_year('2047-11-09'); # my 100th birthday
+day_of_the_year('1752-12-31');
+day_of_the_year('0001-01-01');
+day_of_the_year('9999-12-31');
+
+# today
+my @t = localtime;
+day_of_the_year(sprintf('%04d-%02d-%02d', $t[5] + 1900, $t[4] + 1, $t[3]));
+
+sub day_of_the_year {
+
+ my($y, $m, $d, $day_of_year, @days_in_month);
+
+ # initialise
+ say qq[\nInput: \$date = '$_[0]'];
+ ($y, $m, $d) = $_[0] =~ m|(\d\d\d\d)\-(\d\d)\-(\d\d)|;
+
+ # method 1 - use a module
+ use Date::Calc 'Day_of_Year';
+ say qq[Output 1: ] . Day_of_Year($y, $m, $d);
+
+ # method 2 - use plain Perl
+ @days_in_month = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+
+ # fix February
+ $days_in_month[2] ++ if ($y % 4 == 0 and not ($y % 100 == 0 and $y / 400 % 4 == 0));
+
+ # days in this month plus total of previous months
+ $day_of_year = $d + 0;
+ $day_of_year += $days_in_month[$_] for 1 .. $m - 1;
+ say qq[Output 2: $day_of_year];
+}
diff --git a/challenge-326/peter-campbell-smith/perl/ch-2.pl b/challenge-326/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..0f1a43f68f
--- /dev/null
+++ b/challenge-326/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-16
+use utf8; # Week 326 - task 2 - Decompressed list
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+decompressed_list(1, 3, 2, 4);
+decompressed_list(1, 1, 2, 2);
+decompressed_list(3, 1, 3, 2);
+
+# bigger example
+my @ints;
+push @ints, int(rand(5) + 1) for 0 .. 99;
+decompressed_list(@ints);
+
+sub decompressed_list {
+
+ my (@list, @new_list, $j);
+
+ # initialise
+ @list = @_;
+
+ # do what it says
+ for ($j = 1; $j <= $#list; $j += 2) {
+ push @new_list, $list[$j] for 1 .. $list[$j - 1];
+ }
+
+ # report
+ say qq[\nInput: \@ints1 = (] . join(', ', @list) . q[)];
+ say qq[Output: \@ints2 = (] . join(', ', @new_list) . q[)];
+}