diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-23 19:21:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-23 19:21:28 +0100 |
| commit | a8d5d7744aecbf4dca8665567d9954516d809b66 (patch) | |
| tree | e2616d6cc8a1305237e54ae509a8d77fabf021c3 | |
| parent | 13052ebd3e13452b053d1d4cf4f796712fb02206 (diff) | |
| parent | ade424306f2fa756c28685a54ee32ae013719fa1 (diff) | |
| download | perlweeklychallenge-club-a8d5d7744aecbf4dca8665567d9954516d809b66.tar.gz perlweeklychallenge-club-a8d5d7744aecbf4dca8665567d9954516d809b66.tar.bz2 perlweeklychallenge-club-a8d5d7744aecbf4dca8665567d9954516d809b66.zip | |
Merge pull request #8936 from massa/massa/challenge228
one-liners for challenge 228
| -rw-r--r-- | challenge-228/massa/raku/ch-1.raku | 68 | ||||
| -rw-r--r-- | challenge-228/massa/raku/ch-2.raku | 71 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-228/massa/raku/ch-1.raku b/challenge-228/massa/raku/ch-1.raku new file mode 100644 index 0000000000..c105d1e19c --- /dev/null +++ b/challenge-228/massa/raku/ch-1.raku @@ -0,0 +1,68 @@ +#! /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: Unique Sum + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of integers. + +Write a script to find out the sum of unique elements in the given array. + +=head3 Example 1: + + Input: @int = (2, 1, 3, 2) + Output: 4 + + In the given array we have 2 unique elements (1, 3). + +=head3 Example 2: + + Input: @int = (1, 1, 1, 1) + Output: 0 + + In the given array no unique element found. + +=head3 Example 3: + + Input: @int = (2, 1, 3, 4) + Output: 10 + + In the given array every element is unique. + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub unique-sum(Positional $_) { + .Bag.grep(*.value ≤ 1)».key.sum +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (2, 1, 3, 2), output => 4 }, + %{ input => (1, 1, 1, 1), output => 0 }, + %{ input => (2, 1, 3, 4), output => 10 }, + ]; + + for @tests { + unique-sum( .<input> ).&is-deeply: .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-228/massa/raku/ch-2.raku b/challenge-228/massa/raku/ch-2.raku new file mode 100644 index 0000000000..a23be64440 --- /dev/null +++ b/challenge-228/massa/raku/ch-2.raku @@ -0,0 +1,71 @@ +#! /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: Empty Array + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of integers in which all elements are unique. + +Write a script to perform the following operations until the array is empty and +return the total count of operations. + + If the first element is the smallest then remove it otherwise move it to + the end. + +=head3 Example 1: + + Input: @int = (3, 4, 2) + Ouput: 5 + + Operation 1: move 3 to the end: (4, 2, 3) + Operation 2: move 4 to the end: (2, 3, 4) + Operation 3: remove element 2: (3, 4) + Operation 4: remove element 3: (4) + Operation 5: remove element 4: () + +=head3 Example 2: + + Input: @int = (1, 2, 3) + Ouput: 3 + + Operation 1: remove element 1: (2, 3) + Operation 2: remove element 2: (3) + Operation 3: remove element 3: () + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub empty-array(@_) { + + (@_, -> @ [$head, *@tail] { $head ≤ @tail.all ?? @tail !! [|@tail, $head] } ... * ≤ 1) +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (3, 4, 2), output => 5 }, + %{ input => (1, 2, 3), output => 3 }, + ]; + + for @tests { + empty-array( .<input> ).&is-deeply: .<output>, .<text>; + + } # end of for @tests +} # end of multi MAIN (:$test!) + + |
