From ca202265b90fda5f53c9480ae4f08b5f58f67498 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Sun, 3 Dec 2023 16:34:36 +0000 Subject: Challenge 245 Solutions (Raku) --- challenge-245/mark-anderson/raku/ch-1.raku | 10 +++++ challenge-245/mark-anderson/raku/ch-2.raku | 68 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-245/mark-anderson/raku/ch-1.raku create mode 100644 challenge-245/mark-anderson/raku/ch-2.raku diff --git a/challenge-245/mark-anderson/raku/ch-1.raku b/challenge-245/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..a36321d822 --- /dev/null +++ b/challenge-245/mark-anderson/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Test; + +is-deeply sort-lang(, [2,1,3]), ; +is-deeply sort-lang(, [1,3,2]), ; + +sub sort-lang(@languages, @popularities) +{ + .[@popularities] given [Any, |@languages] +} diff --git a/challenge-245/mark-anderson/raku/ch-2.raku b/challenge-245/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..5bbf9c5699 --- /dev/null +++ b/challenge-245/mark-anderson/raku/ch-2.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +use Test; + +# With help from https://www.geeksforgeeks.org/find-largest-multiple-3-array-digits-set-2-time-o1-space/ + +is largest-of-three(8,1,9), 981; +is largest-of-three(8,6,7,1,0), 8760; +is largest-of-three(1), Nil; + +sub largest-of-three(*@n) +{ + @n .= sort(-*); + + given @n.sum mod 3 + { + when 0 + { + return [~] @n + } + + when 1 + { + my $i = @n.first(* % 3 == 1, :k:end); + + with $i + { + @n.splice($i, 1); + return @n.head ?? [~] @n !! Nil + } + + else + { + return Nil + } + } + + when 2 + { + my $i = @n.first(* % 3 == 2, :k:end); + + with $i + { + @n.splice($i, 1); + return @n.head ?? [~] @n !! Nil + } + + else + { + for (^2) + { + my $i = @n.first(* % 3 == 1, :k:end); + + with $i + { + @n.splice($i, 1); + } + + else + { + return Nil + } + } + + return @n.head ?? [~] @n !! Nil + } + } + } +} -- cgit