aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-25 22:16:27 +0100
committerGitHub <noreply@github.com>2022-09-25 22:16:27 +0100
commit183f4f18f26793cfb999b62102def49f76c4a4c8 (patch)
tree8230df73093efe71c20cd3e718af74a0e744a0d1
parent4366d5686fb510584b27b09cc96808eff462d593 (diff)
parent0bf5a2e01de294e8e523165eaa030c29f4f8238a (diff)
downloadperlweeklychallenge-club-183f4f18f26793cfb999b62102def49f76c4a4c8.tar.gz
perlweeklychallenge-club-183f4f18f26793cfb999b62102def49f76c4a4c8.tar.bz2
perlweeklychallenge-club-183f4f18f26793cfb999b62102def49f76c4a4c8.zip
Merge pull request #6790 from Util/branch-for-challenge-183
Add TWC 183 solutions by Bruce Gray : Raku only.
-rw-r--r--challenge-183/bruce-gray/raku/ch-1.raku16
-rw-r--r--challenge-183/bruce-gray/raku/ch-2.raku46
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-183/bruce-gray/raku/ch-1.raku b/challenge-183/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..ce18b1e250
--- /dev/null
+++ b/challenge-183/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,16 @@
+sub unique-array ( @a ) {
+ return @a.unique( with => &[eqv] );
+}
+
+my @tests =
+ { Input => ( [1,2], [3,4], [5,6], [1,2] ),
+ Expected => ( [1,2], [3,4], [5,6] ) },
+
+ { Input => ( [9,1], [3,7], [2,5], [2,5] ),
+ Expected => ( [9,1], [3,7], [2,5] ) },
+;
+use Test;
+plan +@tests;
+for @tests -> ( :Input(@in), :Expected(@exp) ) {
+ is-deeply unique-array(@in), @exp;
+}
diff --git a/challenge-183/bruce-gray/raku/ch-2.raku b/challenge-183/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..ebd9cb8009
--- /dev/null
+++ b/challenge-183/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,46 @@
+# I would have used .polymod(365), but it won't account for leap years
+sub date-difference ( Str $s1, Str $s2 --> Str ) {
+ my Date ( $d1, $d2 ) = ($s1, $s2).sort.map({ Date.new: $_ });
+
+ my $later-in-year = [lt] map *.Str.substr(4), $d2, $d1;
+
+ my $years = $d2.year - $d1.year - (1 if $later-in-year);
+
+ my $days = $d2 - $d1.later( :$years );
+
+ return join ' ',
+ ( ( "$years year" ~ ('s' if $years > 1) ) if $years ),
+ ( ( "$days day" ~ ('s' if $days > 1) ) if $days ),
+ ;
+}
+
+
+my @tests =
+ ( ( '2019-02-10', '2022-11-01' ), '3 years 264 days' ),
+ ( ( '2020-09-15', '2022-03-29' ), '1 year 195 days' ),
+ ( ( '2019-12-31', '2020-01-01' ), '1 day' ),
+ ( ( '2019-12-01', '2019-12-31' ), '30 days' ),
+ ( ( '2019-12-31', '2020-12-31' ), '1 year' ),
+ ( ( '2019-12-31', '2021-12-31' ), '2 years' ),
+ ( ( '2020-09-15', '2021-09-16' ), '1 year 1 day' ),
+ ( ( '2019-09-15', '2021-09-16' ), '2 years 1 day' ),
+
+ # 2020 was a leap year
+ ( ( '2018-02-28', '2019-02-28' ), '1 year' ),
+ ( ( '2019-02-28', '2020-02-28' ), '1 year' ),
+ ( ( '2020-02-28', '2021-02-28' ), '1 year' ),
+ ( ( '2021-02-28', '2022-02-28' ), '1 year' ),
+
+ ( ( '2018-02-28', '2019-03-01' ), '1 year 1 day' ),
+ ( ( '2019-02-28', '2020-03-01' ), '1 year 2 days' ),
+ ( ( '2020-02-28', '2021-03-01' ), '1 year 1 day' ),
+ ( ( '2021-02-28', '2022-03-01' ), '1 year 1 day' ),
+
+ ( ( '1800-01-01', '1900-01-01' ), '100 years' ),
+
+;
+use Test;
+plan +@tests;
+for @tests -> ( ($s1, $s2), $exp ) {
+ is date-difference($s1, $s2), $exp;
+}