From 69019701d47cc33e5d201616ec8b990aa26dc471 Mon Sep 17 00:00:00 2001 From: rcmlz <19784049+rcmlz@users.noreply.github.com> Date: Fri, 24 Nov 2023 16:33:05 +0100 Subject: ch-244 Raku solutions --- challenge-244/rcmlz/raku/task-one.rakumod | 29 +++++++++++++++++++++++++++++ challenge-244/rcmlz/raku/task-two.rakumod | 15 +++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-244/rcmlz/raku/task-one.rakumod create mode 100644 challenge-244/rcmlz/raku/task-two.rakumod diff --git a/challenge-244/rcmlz/raku/task-one.rakumod b/challenge-244/rcmlz/raku/task-one.rakumod new file mode 100644 index 0000000000..58827215b6 --- /dev/null +++ b/challenge-244/rcmlz/raku/task-one.rakumod @@ -0,0 +1,29 @@ +unit module rcmlz::raku::task-one:ver<0.0.1>:auth:api<1>; + +# run in terminal: raku --optimize=3 -I challenge-nr244/rcmlz/raku/ -- test/challenge-nr244/raku/task-one.rakutest +# or raku --optimize=3 -I challenge-nr244 -- test/benchmark-scalability.raku --task=task-one --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr244; cat /tmp/nr244_task-one.csv + +#|[ +You are given an array of integers. +- Write a script to calculate the number of integers smaller than the integer at each index. + +O(n log(n)) +] +our sub solution(@input) is export { + my @output[@input.elems]; + + my @sorted = @input.sort(:k); + my %new-old = @sorted.pairs; + my %old-new = @sorted.antipairs; + + # we avoid O(n^2) solution by using double mapping of sorted ids of input array + for @input.kv -> $k,$v { + my $pos = %old-new{$k}; + while $pos > 0 && @input[%new-old{$pos - 1}] == $v { + $pos--; + } + @output[$k] = $pos; + + } + return @output; +} \ No newline at end of file diff --git a/challenge-244/rcmlz/raku/task-two.rakumod b/challenge-244/rcmlz/raku/task-two.rakumod new file mode 100644 index 0000000000..2bbc58ce4a --- /dev/null +++ b/challenge-244/rcmlz/raku/task-two.rakumod @@ -0,0 +1,15 @@ +unit module rcmlz::raku::task-two:ver<0.0.1>:auth:api<1>; + +# run in terminal: raku --optimize=3 -I challenge-nr244/rcmlz/raku/ -- test/challenge-nr244/raku/task-two.rakutest +# or raku --optimize=3 -I challenge-nr244 -- test/benchmark-scalability.raku --task=task-two --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr244; cat /tmp/nr244_task-two.csv + +#|[ +You are given an array of integers representing the strength. +- Write a script to return the sum of the powers of all possible combinations; +- power is defined as the square of the largest number in a sequence, multiplied by the smallest. +] +our sub solution(@input) is export { + [+] @input.combinations + .grep( *.elems ) + .map( { .min * .max**2 } ) +} \ No newline at end of file -- cgit