diff options
| author | rir <rirans@comcast.net> | 2023-09-17 19:46:18 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2023-09-17 19:46:18 -0400 |
| commit | c368831f11db0311e9f1df0fd5adc47b31dcbd03 (patch) | |
| tree | 79c1ef18c538ebdae4500a051400c1515510de5b | |
| parent | 237c1d2d70d40bbd7f05fcbbe77aace8c86735ba (diff) | |
| download | perlweeklychallenge-club-c368831f11db0311e9f1df0fd5adc47b31dcbd03.tar.gz perlweeklychallenge-club-c368831f11db0311e9f1df0fd5adc47b31dcbd03.tar.bz2 perlweeklychallenge-club-c368831f11db0311e9f1df0fd5adc47b31dcbd03.zip | |
234 ch-1.raku
| -rw-r--r-- | challenge-234/0rir/raku/ch-1.raku | 71 |
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; + |
