diff options
| author | Humberto Massa <humbertomassa@gmail.com> | 2023-09-11 14:31:47 -0300 |
|---|---|---|
| committer | Humberto Massa <humbertomassa@gmail.com> | 2023-09-11 14:31:47 -0300 |
| commit | 8bedf705aff1de8422eb24e022bc72e24f87ee67 (patch) | |
| tree | 5eb14e0cef18ca6b980cbd306ebd91af6fe643ad /challenge-234 | |
| parent | 3c9efcab101672b5ecfa042ca258f93d81474928 (diff) | |
| download | perlweeklychallenge-club-8bedf705aff1de8422eb24e022bc72e24f87ee67.tar.gz perlweeklychallenge-club-8bedf705aff1de8422eb24e022bc72e24f87ee67.tar.bz2 perlweeklychallenge-club-8bedf705aff1de8422eb24e022bc72e24f87ee67.zip | |
One-liner solutions
Diffstat (limited to 'challenge-234')
| -rw-r--r-- | challenge-234/massa/raku/ch-1.raku | 64 | ||||
| -rw-r--r-- | challenge-234/massa/raku/ch-2.raku | 65 |
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-234/massa/raku/ch-1.raku b/challenge-234/massa/raku/ch-1.raku new file mode 100644 index 0000000000..e81c2851dd --- /dev/null +++ b/challenge-234/massa/raku/ch-1.raku @@ -0,0 +1,64 @@ +#! /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: Common Characters + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of words made up of alphabetic characters only. + +Write a script to return all alphabetic characters that show up in all words including duplicates. + +=head3 Example 1: + +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max. + +=head3 Example 2: + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +=head3 Example 3: + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub common-characters(@_) { + ([∩] @_».comb».Bag).kv.map({ $^a xx $^b }).flat +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => <java javascript julia>, output => <j a> }, + %{ input => <bella label roller>, output => <e l l> }, + %{ input => <cool lock cook>, output => <c o> }, + ]; + + for @tests { + common-characters( .<input> ).&is-deeply: .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-234/massa/raku/ch-2.raku b/challenge-234/massa/raku/ch-2.raku new file mode 100644 index 0000000000..6c7b65545b --- /dev/null +++ b/challenge-234/massa/raku/ch-2.raku @@ -0,0 +1,65 @@ +#! /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: Unequal Triplets + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +Input: @ints = (4, 4, 2, 4, 3) +Ouput: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +=head3 Example 1: + +Input: @ints = (1, 1, 1, 1, 1) +Ouput: 0 + +=head3 Example 2: + +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +triplets of 1, 4, 7 = 3x2×2 = 12 combinations +triplets of 1, 4, 10 = 3×2×1 = 6 combinations +triplets of 4, 7, 10 = 2×2×1 = 4 combinations +triplets of 1, 7, 10 = 3x2x1 = 6 combinations + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub unequal-triplets(@_) { + (^+@_).combinations(3).grep({@_[$_].Bag.elems == 3}).elems +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (4, 4, 2, 4, 3), output => 3 }, + %{ input => (1, 1, 1, 1, 1), output => 0 }, + %{ input => (4, 7, 1, 10, 7, 4, 1, 1), output => 28 }, + ]; + + for @tests { + unequal-triplets( .<input> ).&is-deeply: .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (:$test!) + + |
