From 8bedf705aff1de8422eb24e022bc72e24f87ee67 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Mon, 11 Sep 2023 14:31:47 -0300 Subject: One-liner solutions --- challenge-234/massa/raku/ch-1.raku | 64 +++++++++++++++++++++++++++++++++++++ challenge-234/massa/raku/ch-2.raku | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 challenge-234/massa/raku/ch-1.raku create mode 100644 challenge-234/massa/raku/ch-2.raku 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 => , output => }, + %{ input => , output => }, + %{ input => , output => }, + ]; + + for @tests { + common-characters( . ).&is-deeply: ., .; + } # 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( . ).&is-deeply: ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + + -- cgit