diff options
| author | rcmlz <19784049+rcmlz@users.noreply.github.com> | 2023-11-24 16:33:05 +0100 |
|---|---|---|
| committer | rcmlz <19784049+rcmlz@users.noreply.github.com> | 2023-11-24 16:33:05 +0100 |
| commit | 69019701d47cc33e5d201616ec8b990aa26dc471 (patch) | |
| tree | cfae2ebb17e893d1dcc61c734c9f7a6720eb1e74 | |
| parent | d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5 (diff) | |
| download | perlweeklychallenge-club-69019701d47cc33e5d201616ec8b990aa26dc471.tar.gz perlweeklychallenge-club-69019701d47cc33e5d201616ec8b990aa26dc471.tar.bz2 perlweeklychallenge-club-69019701d47cc33e5d201616ec8b990aa26dc471.zip | |
ch-244 Raku solutions
| -rw-r--r-- | challenge-244/rcmlz/raku/task-one.rakumod | 29 | ||||
| -rw-r--r-- | challenge-244/rcmlz/raku/task-two.rakumod | 15 |
2 files changed, 44 insertions, 0 deletions
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<github:rcmlz>: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<github:rcmlz>: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 |
