aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-18 00:56:40 +0100
committerGitHub <noreply@github.com>2023-09-18 00:56:40 +0100
commitb9ed102d905671f6696f37f6ba77f305155a1997 (patch)
tree8d8872b44610c4b0b11a70955066ba82d5757d26
parentd70c6af225f654081884e720449f92c0f52514b1 (diff)
parentc368831f11db0311e9f1df0fd5adc47b31dcbd03 (diff)
downloadperlweeklychallenge-club-b9ed102d905671f6696f37f6ba77f305155a1997.tar.gz
perlweeklychallenge-club-b9ed102d905671f6696f37f6ba77f305155a1997.tar.bz2
perlweeklychallenge-club-b9ed102d905671f6696f37f6ba77f305155a1997.zip
Merge pull request #8719 from 0rir/234
234 ch-1.raku
-rw-r--r--challenge-234/0rir/raku/ch-1.raku71
1 files changed, 71 insertions, 0 deletions
diff --git a/challenge-234/0rir/raku/ch-1.raku b/challenge-234/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..8fd07459d2
--- /dev/null
+++ b/challenge-234/0rir/raku/ch-1.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env rak;
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ;
+use v6.d;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+234-1: Common Characters
+Submitted by: Mohammad S Anwar
+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.
+
+Example 1
+Input: @words = ("java", "javascript", "julia")
+Output: ("j", "a")
+Example 2
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+Example 3
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+
+=end comment
+
+my @Test =
+ ("j" => 1, "a" =>1).sort, ("java", "javascript", "julia"),
+ ("e" => 1, "l" =>2), ("bella", "label", "roller"),
+ ("c" => 1, "o" =>1), ("cool", "lock", "cook"),
+;
+
+plan @Test ÷ 2;
+
+func();
+
+# returns array of pair
+sub func( @i = @Test[1] --> Seq) {
+ my @char = @i.join.comb.sort.unique;
+ my $hash;
+
+ for ^@i -> $i {
+ for @i[$i].comb -> $c { ++ $hash{$c}[$i]; }
+ }
+
+ my @return;
+ for $hash.kv -> $k, @v {
+ for @v { $_ //= 0; }
+ next if @v.elems < @i.elems;
+ @return.push: $k => @v.min if @v.min > 0;
+ }
+ @return.sort;
+}
+
+sub _str( @in --> Str ) {
+ my @return;
+ for @in -> $p {
+ @return.push( $p.key ) for ^$p.value;
+ }
+ @return.sort.join( ", ");
+}
+
+for @Test -> @exp, @in {
+ is func(@in), @exp, "@exp.raku() <- @in.raku()";
+}
+
+done-testing;
+
+my @word = "bella", "label", "roller";
+say "\nInput: @word = @word.raku()\nOutput: &_str(func( @word))";
+exit;
+