diff options
| -rw-r--r-- | challenge-277/0rir/raku/ch-1.raku | 60 | ||||
| -rw-r--r-- | challenge-277/0rir/raku/ch-2.raku | 57 |
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-277/0rir/raku/ch-1.raku b/challenge-277/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..dcdb976844 --- /dev/null +++ b/challenge-277/0rir/raku/ch-1.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +277-1: Count Common Submitted by: Mohammad Sajid Anwar +You are given two array of strings, @words1 and @words2. Return the count +of words that appears in both arrays exactly once. + +Example 1 +Input: @words1 = ("Perl", "is", "my", "friend") + @words2 = ("Perl", "and", "Raku", "are", "friend") +Output: 2 + +The words "Perl" and "friend" appear once in each array. +Example 2 +Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar") + @words2 = ("Python", "is", "top", "in", "guest", "languages") +Output: 1 +Example 3 +Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional") + @words2 = ("Crystal", "is", "similar", "to", "Ruby") +Output: 0 + +=end comment + +my @Test = + # exe list / list + 0, (), (), + 0, (), < Pi is positive and is long >, + 1, ('red',), ('Red', 'dog'), + 2, ("Perl", "is", "my", "friend"), + ("Perl", "and", "Raku", "are", "friend"), + 1, ("Perl", "and", "Python", "are", "very", "similar"), + ("Python", "is", "top", "in", "guest", "languages"), + 0, ("Perl", "is", "imperative", "Lisp", "is", "functional"), + ("Crystal", "is", "similar", "to", "Ruby"), +; +plan @Test ÷ 3; + +sub task( @a, @b --> Int) { + ( (@a».fc).BagHash.grep( *.value == 1 ) + ∩ + (@b».fc).BagHash.grep( *.value == 1 ) + ).elems; +} + +for @Test -> $exp, @a, @b, { + is task(@a, @b), $exp, "$exp <- @a[] +/+ @b[]"; +} + +done-testing; + +my @word1 = < I am positive>; +my @word2 = < Pi is positive>; + +say "\nInput: @words1 = @word1.List.raku()" + ~ "\n @words2 = @word2.List.raku()" + ~ "\n Output: ", task(@word1, @word2); diff --git a/challenge-277/0rir/raku/ch-2.raku b/challenge-277/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..beed29ebc8 --- /dev/null +++ b/challenge-277/0rir/raku/ch-2.raku @@ -0,0 +1,57 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +277-2: Strong Pair Submitted by: Mohammad Sajid Anwar +Given an array of integers, @ints. Return the count of all strong +pairs in the given array. + +A pair of integers x and y is called strong pair if it satisfies: + 0 < |x - y| < min(x, y). + +Example 1 +Input: @ints = (1, 2, 3, 4, 5) +Ouput: 4 + +Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) +Example 2 +Input: @ints = (5, 7, 1, 7) +Ouput: 1 + +Strong Pairs: (5, 7) +=end comment + +my @Test = + # exp in + Int, List, + Int, Array, + 0, (), + 0, (1,), + 0, (1,-1), + 4, (1, 2, 3, 4, 5), + 1, (5, 7, 1, 7), + 1, (2, 3), + 0, (1, 2), + 0, (1, 30,), +; +plan @Test ÷ 2; + +only task( @in -->Int) { + return Int unless @in.defined; + my @a = @in.grep( * > 1).unique; + return 0 if +@a == (0,1).any; + +@a.combinations(2).grep( { 0 < ($_[0] - $_[1]).abs < $_.min}); +} + +for @Test -> $exp, @in { + is task(@in), $exp, "$exp.gist() <- @in.gist()"; +} + +done-testing; + +# 0 < |x - y| < min(x, y). +my @int = ( -9, -7, -5, 0, 0, 1, 1, 2, 5, 7, 8, 8); +say "\nInput: @ints = @int.raku()\nOuput: &task(@int)"; + |
