aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-09-29 21:22:52 +0100
committerGitHub <noreply@github.com>2021-09-29 21:22:52 +0100
commit229268b52a245e49f557bbc92255b46f7e5cb148 (patch)
tree6bf7b01d7fb4f7c0e8f645b49a31deda5a3c6470
parent34d06d547b6cd46081abf32fe4bb943a3dd7239f (diff)
parent727e554c0afbbba7e6a977704d0be3c57f6c2674 (diff)
downloadperlweeklychallenge-club-229268b52a245e49f557bbc92255b46f7e5cb148.tar.gz
perlweeklychallenge-club-229268b52a245e49f557bbc92255b46f7e5cb148.tar.bz2
perlweeklychallenge-club-229268b52a245e49f557bbc92255b46f7e5cb148.zip
Merge pull request #4943 from Kaiepi/challenge-132
Add Ben Davies' Raku solutions for challenge 132
-rw-r--r--challenge-132/ben-davies/raku/ch-1.raku11
-rw-r--r--challenge-132/ben-davies/raku/ch-2.raku33
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-132/ben-davies/raku/ch-1.raku b/challenge-132/ben-davies/raku/ch-1.raku
new file mode 100644
index 0000000000..5836cfc91e
--- /dev/null
+++ b/challenge-132/ben-davies/raku/ch-1.raku
@@ -0,0 +1,11 @@
+use v6;
+unit sub MAIN(
+ #|[ A yyyy/mm/dd formatted birthday. ]
+ Str:D $input where ?m/ ^ $<year>=[\d+] '/' $<month>=[\d ** 2] '/' $<day>=[\d ** 2] $ /
+) {
+ my Date:D $birthday .= new: +$<year>, +$<month>, +$<day>, :&formatter;
+ my Date:D $today .= new: 2021, 9, 22, :&formatter;
+ my Int:D $lifespan = $today - $birthday;
+ put $birthday - $lifespan, ', ', $today + $lifespan;
+ sub formatter(Dateish:D $date --> Str:D) { '%d/%02d/%02d'.sprintf: $date.year, $date.month, $date.day }
+}
diff --git a/challenge-132/ben-davies/raku/ch-2.raku b/challenge-132/ben-davies/raku/ch-2.raku
new file mode 100644
index 0000000000..ea037c976c
--- /dev/null
+++ b/challenge-132/ben-davies/raku/ch-2.raku
@@ -0,0 +1,33 @@
+use v6;
+
+#|[ Performs a hash join given the probe input, build input, and corresponding
+ indices with which to compare. ]
+sub hashjoin(@S, @R, Int:D $si, Int:D $ri) {
+ # 1. For each tuple r in the build input R
+ # 1.1 Add r to the in-memory hash table
+ @R.classify({ $^r[$ri] }, :into(my %hash{Any}));
+ # 2. Do a final scan of the probe input S and add the resulting join tuples
+ # to the output relation
+ @S.sort({ $^a[$si] cmp $^b[$si] }).map({
+ %hash{$^s[$si]} andthen |.map({ |$s, |$^r[^$ri], |$^r[$ri^..*] }) })
+}
+
+my @player-ages = (
+ [20, "Alex" ],
+ [28, "Joe" ],
+ [38, "Mike" ],
+ [18, "Alex" ],
+ [25, "David" ],
+ [18, "Simon" ],
+);
+
+my @player-names = (
+ ["Alex", "Stewart"],
+ ["Joe", "Root" ],
+ ["Mike", "Gatting"],
+ ["Joe", "Blog" ],
+ ["Alex", "Jones" ],
+ ["Simon","Duane" ],
+);
+
+sub MAIN() { .put for hashjoin @player-ages, @player-names, 1, 0 }