blob: 700f81ed74d2ef00f208d00a49a21cadcffac776 (
plain)
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
|
#!/usr/bin/env raku
use v6.d;
sub replace-all-q(Str $str --> Str) {
return $str unless $str.contains('?');
my @chars = $str.comb;
if @chars[0] eq '?' {
@chars[0] = @chars[1] eq 'a' ?? 'b' !! 'a';
}
for 1..@chars.elems - 2 {
if @chars[$_] eq '?' {
my $surroundings = Set(@chars[$_ - 1], @chars[$_ + 1]);
if 'a' ∉ $surroundings {
@chars[$_] = 'a';
} elsif 'b' ∉ $surroundings {
@chars[$_] = 'b';
} else {
@chars[$_] = 'c';
}
}
}
@chars.join;
}
#| Run test cases
multi sub MAIN('test') {
use Test;
plan 3;
is replace-all-q("a?z"), "abz", 'works for "a?z"';
is replace-all-q("pe?k"), "peak", 'works for "pe?k"';
is replace-all-q("gra?te"), "grabte", 'works for "gra?te"';
}
#| Take user provided string like "a?z"
multi sub MAIN(Str $str) {
say replace-all-q($str);
}
|