diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-17 23:31:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-17 23:31:17 +0100 |
| commit | 331c5346d87f3131e7480194e98b570d7ff3d6de (patch) | |
| tree | 62a64951e7b52df8378fcf2d6a1146fa9738e35b | |
| parent | b64d7cc997e44a52d797c1040883cdab309fc458 (diff) | |
| parent | 237c1d2d70d40bbd7f05fcbbe77aace8c86735ba (diff) | |
| download | perlweeklychallenge-club-331c5346d87f3131e7480194e98b570d7ff3d6de.tar.gz perlweeklychallenge-club-331c5346d87f3131e7480194e98b570d7ff3d6de.tar.bz2 perlweeklychallenge-club-331c5346d87f3131e7480194e98b570d7ff3d6de.zip | |
Merge pull request #8718 from 0rir/234
234 ch-2.raku
| -rw-r--r-- | challenge-234/0rir/raku/ch-2.raku | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-234/0rir/raku/ch-2.raku b/challenge-234/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..4edc797138 --- /dev/null +++ b/challenge-234/0rir/raku/ch-2.raku @@ -0,0 +1,54 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +234-2: Unequal Triplets Submitted by: Mohammad S Anwar +You are given an array of positive integers. + +Write a script to find the number of triplets (i, j, k) that satisfies num[i] != num[j], num[j] != num[k] and num[k] != num[i]. + +Example 1 +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 +Example 2 +Input: @ints = (1, 1, 1, 1, 1) +Ouput: 0 +Example 3 +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 +=end comment + +my @Test = + 3, (4, 4, 2, 4, 3), + 0, (1, 1, 1, 1, 1), + 28, (4, 7, 1, 10, 7, 4, 1, 1), +; + +plan @Test/2; + +sub func( @a = @Test[1]) { + my @c = @a.combinations(3).grep( { + $_[0] ≠ $_[1] + and $_[1] ≠ $_[2] + and $_[2] ≠ $_[0] } + ).elems; +} + +for @Test -> $exp, @in { + is func(@in), $exp, "$exp <- @in.raku()"; +} +done-testing; +exit; + |
