diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-19 21:55:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-19 21:55:45 +0000 |
| commit | be8d0a4be9468013a6b44411044878d7a059af1f (patch) | |
| tree | 64963484531f1d7c0b459e07dded9f4a396cb1a5 /challenge-243 | |
| parent | 09ed97284bea0d122fb6f5e57a3b6520e825a206 (diff) | |
| parent | 0517794ddd39ca5d18018c13461e4e724773c3e2 (diff) | |
| download | perlweeklychallenge-club-be8d0a4be9468013a6b44411044878d7a059af1f.tar.gz perlweeklychallenge-club-be8d0a4be9468013a6b44411044878d7a059af1f.tar.bz2 perlweeklychallenge-club-be8d0a4be9468013a6b44411044878d7a059af1f.zip | |
Merge pull request #9092 from massa/challenge243
Oneliners as ever
Diffstat (limited to 'challenge-243')
| -rw-r--r-- | challenge-243/massa/raku/ch-1.raku | 66 | ||||
| -rw-r--r-- | challenge-243/massa/raku/ch-2.raku | 67 |
2 files changed, 133 insertions, 0 deletions
diff --git a/challenge-243/massa/raku/ch-1.raku b/challenge-243/massa/raku/ch-1.raku new file mode 100644 index 0000000000..92be046bab --- /dev/null +++ b/challenge-243/massa/raku/ch-1.raku @@ -0,0 +1,66 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Reverse Pairs + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given two arrays of integers. + +Write a script to return the number of reverse pairs in the given array. + +A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) +nums[i] > 2 * nums[j]. + +=head3 Example 1: + + Input: @nums = (1, 3, 2, 3, 1) + Output: 2 + + (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 + (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 + +=head3 Example 2: + + Input: @nums = (2, 4, 3, 5, 1) + Output: 3 + + (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 + (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 + (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@m) { + + @m.pairs.combinations(2).grep({.[0].value > 2 * .[1].value})».key +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = + %{ input => (1, 3, 2, 3, 1), + output => (2,) }, + %{ input => (2, 4, 3, 5, 1), + output => (3,) }, + ; + + .<input>.&SOLUTION.deepmap({$_}).&is-deeply: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-243/massa/raku/ch-2.raku b/challenge-243/massa/raku/ch-2.raku new file mode 100644 index 0000000000..2899677d8b --- /dev/null +++ b/challenge-243/massa/raku/ch-2.raku @@ -0,0 +1,67 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Floor Sum + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of positive integers (>=1). + +Write a script to return the sum of floor(nums[i] / nums[j]) where 0 <= i,j < +nums.length. The floor() function returns the integer part of the division. + +=head3 Example 1: + + Input: @nums = (2, 5, 9) + Output: 10 + + floor(2 / 5) = 0 + floor(2 / 9) = 0 + floor(5 / 9) = 0 + floor(2 / 2) = 1 + floor(5 / 5) = 1 + floor(9 / 9) = 1 + floor(5 / 2) = 2 + floor(9 / 2) = 4 + floor(9 / 5) = 1 + +=head3 Example 2: + + Input: @nums = (7, 7, 7, 7, 7, 7, 7) + Output: 49 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@_) { + [+] @_ Xdiv @_ +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = + %{ input => (2, 5, 9), + output => (10,) }, + %{ input => (7, 7, 7, 7, 7, 7, 7), + output => (49,) }, + ; + + .<input>.&SOLUTION.deepmap({$_}).&is-deeply: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + |
