diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-24 22:10:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-24 22:10:08 +0000 |
| commit | e524b502bf93e292a7c889f84b9869c2adf086a4 (patch) | |
| tree | 8f09b5a0619bbd64c65ef490ccf27e14c88ce8e1 | |
| parent | c05edc10016fcc6769ef2cd764ff05f6d1f17181 (diff) | |
| parent | 69019701d47cc33e5d201616ec8b990aa26dc471 (diff) | |
| download | perlweeklychallenge-club-e524b502bf93e292a7c889f84b9869c2adf086a4.tar.gz perlweeklychallenge-club-e524b502bf93e292a7c889f84b9869c2adf086a4.tar.bz2 perlweeklychallenge-club-e524b502bf93e292a7c889f84b9869c2adf086a4.zip | |
Merge pull request #9125 from rcmlz/ch-244
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 |
