aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-20 23:27:54 +0100
committerGitHub <noreply@github.com>2025-06-20 23:27:54 +0100
commitd909b7dadedada08fa9383b744331d5f9e7a54e9 (patch)
tree4529662bbdebcf101f012b884e778cf28a5d7f15
parentee778068e2a9c1807a8317499f1ae19e0caa21d0 (diff)
parentf346c1cf6d36df41c0c447e8fb8eb0743a27cecf (diff)
downloadperlweeklychallenge-club-d909b7dadedada08fa9383b744331d5f9e7a54e9.tar.gz
perlweeklychallenge-club-d909b7dadedada08fa9383b744331d5f9e7a54e9.tar.bz2
perlweeklychallenge-club-d909b7dadedada08fa9383b744331d5f9e7a54e9.zip
Merge pull request #12205 from wanderdoc/master
PWC 326 (wanderdoc)
-rw-r--r--challenge-326/wanderdoc/perl/ch-1.pl45
-rw-r--r--challenge-326/wanderdoc/perl/ch-2.pl55
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-326/wanderdoc/perl/ch-1.pl b/challenge-326/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..8b7f83300f
--- /dev/null
+++ b/challenge-326/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+
+=cut
+
+
+
+use Time::Piece;
+use Test2::V0 -no_srand => 1;
+is(str_to_day_of_year('2025-02-02'), 33, 'Example 1');
+is(str_to_day_of_year('2025-04-10'), 100, 'Example 2');
+is(str_to_day_of_year('2025-09-07'), 250, 'Example 3');
+done_testing();
+
+sub str_to_day_of_year
+{
+ my $string = $_[0];
+ my $format = '%Y-%m-%d';
+ my $dt = Time::Piece->strptime($string, $format);
+ return $dt->day_of_year + 1; # $dt->day_of_year: 0 = Jan 01
+}
diff --git a/challenge-326/wanderdoc/perl/ch-2.pl b/challenge-326/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..5f1d5d3ca2
--- /dev/null
+++ b/challenge-326/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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)
+
+=cut
+
+
+
+
+
+use Test2::V0 -no_srand => 1;
+
+is(decompressed_list(1, 3, 2, 4), [3, 4, 4], 'Example 1');
+is(decompressed_list(1, 1, 2, 2), [1, 2, 2], 'Example 1');
+is(decompressed_list(3, 1, 3, 2), [1, 1, 1, 2, 2, 2], 'Example 3');
+done_testing();
+
+sub decompressed_list
+{
+ my @arr = @_;
+ return
+ [map { ($arr[$_ + 1]) x $arr[$_] }
+ grep { $_ % 2 == 0 }
+ 0 .. $#arr];
+}