/
ch-2.raku
65 lines (53 loc) · 1.63 KB
/
ch-2.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env raku
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴
use v6.c;
use Test;
=begin comment
239-2: Consistent Strings Submitted by: Mohammad S Anwar
Given an array of strings and allowed string having distinct characters.
A string is consistent if all characters in the string appear in the
string allowed.
Write a script to return the number of consistent strings in the given array.
Example 1
Input: @str = ("ad", "bd", "aaab", "baa", "badab")
$allowed = "ab"
Output: 2
Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Example 2
Input: @str = ("a", "b", "c", "ab", "ac", "bc", "abc")
$allowed = "abc"
Output: 7
Example 3
Input: @str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d")
$allowed = "cad"
Output: 4
Strings "cc", "acd", "ac", and "d" are consistent.
=end comment
my @Test =
{ str => <ad bd aaab baa badab>,
valid => "ab",
exp => 2,
},
{ str => ("a", "b", "c", "ab", "ac", "bc", "abc"),
valid => "abc",
exp => 7,
},
{ str => ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d"),
valid => "cad",
exp => 4,
},
;
plan +@Test;
sub func( @str, $valid --> Any) {
my $ok = $valid.comb.Set;
@str.map( *.comb.Set ⊆ $ok).grep( *.so).elems;
}
for @Test -> %h {
is func(%h<str>, %h<valid>), %h<exp>, "%h<exp> <= %h<valid> <- %h<str>";
}
done-testing;
my @str = < alfhja asdflkj alsdjf salkjf lssksaj >;
my $allowed = "alsdkfj";
say "\nInput: @str = @str[]\n \$allowed = $allowed\nOutput: ",
&func( @str, $allowed);
exit;