aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-21 21:29:02 +0100
committerGitHub <noreply@github.com>2022-09-21 21:29:02 +0100
commitf45c19c8c1a51355e2cedb107293fbfff80d7003 (patch)
tree6aeb3c775ba30d8b277846413f9960c01e58eff5
parent06903e154012dd6da0cedbc803d702fea9334492 (diff)
parent64a818705933d415c4f77d22b0e404c5d250a6c0 (diff)
downloadperlweeklychallenge-club-f45c19c8c1a51355e2cedb107293fbfff80d7003.tar.gz
perlweeklychallenge-club-f45c19c8c1a51355e2cedb107293fbfff80d7003.tar.bz2
perlweeklychallenge-club-f45c19c8c1a51355e2cedb107293fbfff80d7003.zip
Merge pull request #6775 from PerlBoy1967/branch-for-challenge-183
w183
-rwxr-xr-xchallenge-183/perlboy1967/ch-2.pl60
-rwxr-xr-xchallenge-183/perlboy1967/perl/ch-1.pl62
-rwxr-xr-xchallenge-183/perlboy1967/perl/ch-2.pl64
3 files changed, 186 insertions, 0 deletions
diff --git a/challenge-183/perlboy1967/ch-2.pl b/challenge-183/perlboy1967/ch-2.pl
new file mode 100755
index 0000000000..12e50fc040
--- /dev/null
+++ b/challenge-183/perlboy1967/ch-2.pl
@@ -0,0 +1,60 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 183
+ - https://theweeklychallenge.org/blog/perl-weekly-challenge-183/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Date Difference
+Submitted by: Mohammad S Anwar
+
+You are given two dates, $date1 and $date2 in the format YYYY-MM-DD.
+
+Write a script to find the difference between the given dates in terms on years and days only.
+
+=cut
+
+use v5.16;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+use Data::Printer output => 'stdout';
+
+use DateTime;
+use DateTime::Duration;
+
+
+sub dateToHash($) {
+ return {year => $1, month => $2, day => $3}
+ if ($_[0] =~ /^(\d+)-(\d+)-(\d+)$/);
+}
+
+sub dateDifference($$) {
+ my ($f,$t) = map { DateTime->new(dateToHash $_) } sort @_;
+
+ my $y = $t->subtract_datetime($f)->in_units('years');
+ $f += DateTime::Duration->new(years => $y);
+
+ return [$y, $f->delta_days($t)->{days}];
+}
+
+my @d = (
+ ['2019-02-10','2022-11-01',[3,264]],
+ ['2020-09-15','2022-03-29',[1,195]],
+ ['2019-12-31','2020-01-01',[0,1]],
+ ['2019-12-01','2019-12-31',[0,30]],
+ ['2019-12-31','2020-12-31',[1,0]],
+ ['2019-12-31','2021-12-31',[2,0]],
+ ['2020-09-15','2021-09-16',[1,1]],
+ ['2019-09-15','2021-09-16',[2,1]],
+);
+
+foreach my $c (@d) {
+ cmp_deeply(dateDifference($$c[0],$$c[1]),$c->[2]);
+ cmp_deeply(dateDifference($$c[1],$$c[0]),$c->[2]);
+}
+
+done_testing;
diff --git a/challenge-183/perlboy1967/perl/ch-1.pl b/challenge-183/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..0311101076
--- /dev/null
+++ b/challenge-183/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 183
+ - https://theweeklychallenge.org/blog/perl-weekly-challenge-183/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Unique Array
+Submitted by: Mohammad S Anwar
+
+You are given list of arrayrefs.
+
+Write a script to remove the duplicate arrayrefs from the given list.
+
+=cut
+
+use v5.16;
+use warnings;
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+use Data::Compare qw();
+
+####################################################################
+# sub uuniq(@list)
+#
+# Description: Returns a unique list regardlessly list element types
+# Name: Universal uniq => 'uuniq'
+####################################################################
+sub uuniq (@) {
+ my @r;
+ map {
+ my $r = $_;
+ push(@r,$_)
+ if (!grep { Data::Compare::Compare($r,$_) } @r);
+ } @_;
+ return @r;
+}
+
+# Simple 'uniq'
+cmp_deeply([uuniq(1,2,3,4,5,4,3,2,1)],
+ [1,2,3,4,5]);
+# Uniq list of lists
+cmp_deeply([uuniq([1,2],[3,4],[5,6],[1,2])],
+ [[1,2],[3,4],[5,6]]);
+cmp_deeply([uuniq([9,1],[3,7],[2,5],[2,5])],
+ [[9,1],[3,7],[2,5]]);
+cmp_deeply([uuniq([1,2],[1,2,3],[1,2],[2,3])],
+ [[1,2],[1,2,3],[2,3]]);
+# Uniq list of hashes
+cmp_deeply([uuniq({a=>1},{b=>2},{a=>1})],
+ [{a=>1},{b=>2}]);
+cmp_deeply([uuniq({a=>1},{a=>1,b=>2},{b=>2},{a=>1})],
+ [{a=>1},{a=>1,b=>2},{b=>2}]);
+# Going wild.... :-)
+cmp_deeply([uuniq(1,[1,2],2,[2,3],2,{a=>1},{b=>1},{a=>1},[1,2])],
+ [1,[1,2],2,[2,3],{a=>1},{b=>1}]);
+
+done_testing();
diff --git a/challenge-183/perlboy1967/perl/ch-2.pl b/challenge-183/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..513f76d87a
--- /dev/null
+++ b/challenge-183/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 183
+ - https://theweeklychallenge.org/blog/perl-weekly-challenge-183/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Date Difference
+Submitted by: Mohammad S Anwar
+
+You are given two dates, $date1 and $date2 in the format YYYY-MM-DD.
+
+Write a script to find the difference between the given dates in terms on years and days only.
+
+=cut
+
+use v5.16;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+
+use DateTime;
+use DateTime::Duration;
+
+
+sub dateToHash($) {
+ return {year => $1, month => $2, day => $3}
+ if ($_[0] =~ /^(\d+)-(\d+)-(\d+)$/);
+}
+
+sub dateDifference($$) {
+ my ($f,$t) = sort map { DateTime->new(dateToHash $_) } @_;
+
+ my $year = DateTime::Duration->new(years => 1);
+
+ my $y = 0;
+ while ($f <= $t - $year) {
+ $f += $year;
+ $y++;
+ }
+
+ return [$y,$f->delta_days($t)->{days}];
+}
+
+my @d = (
+ ['2019-02-10','2022-11-01',[3,264]],
+ ['2020-09-15','2022-03-29',[1,195]],
+ ['2019-12-31','2020-01-01',[0,1]],
+ ['2019-12-01','2019-12-31',[0,30]],
+ ['2019-12-31','2020-12-31',[1,0]],
+ ['2019-12-31','2021-12-31',[2,0]],
+ ['2020-09-15','2021-09-16',[1,1]],
+ ['2019-09-15','2021-09-16',[2,1]],
+);
+
+foreach my $c (@d) {
+ cmp_deeply(dateDifference($$c[0],$$c[1]),$c->[2]);
+ cmp_deeply(dateDifference($$c[1],$$c[0]),$c->[2]);
+}
+
+done_testing;