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
65
66
67
68
69
|
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2024, Bob Lied
#=============================================================================
# ch-2.pl Perl Weekly Challenge 273 Task 2 B after A
#=============================================================================
# You are given a string, $str.
# Write a script to return true if there is at least one b, and no a
# appears after the first b.
# Example 1 Input: $str = "aabb" Output: true
# Example 2 Input: $str = "abab" Output: false
# Example 3 Input: $str = "aaa" Output: false
# Example 4 Input: $str = "bbb" Output: true
#=============================================================================
use v5.40;
use Getopt::Long;
my $DoTest = false;
my $Benchmark = 0;
GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark);
exit(!runTest()) if $DoTest;
exit( runBenchmark($Benchmark) ) if $Benchmark;
say ( bAfterA($_) ? "true" : "false" ) for @ARGV;
sub bAfterA($str)
{
my $w = index($str, "b");
return $w >= 0 && index($str, "a", $w) < 0;
}
sub bAfterA_RE($str)
{
$str =~ m/^[^b]*b[^a]*$/
}
sub runTest
{
use Test2::V0;
is (bAfterA("aabb"), true, "Example 1");
is (bAfterA("abab"), false, "Example 2");
is (bAfterA("aaa" ), false, "Example 3");
is (bAfterA("bbb" ), true, "Example 4");
is (bAfterA_RE("aabb"), true, "Example 1 RE");
is (bAfterA_RE("abab"), false, "Example 2 RE");
is (bAfterA_RE("aaa" ), false, "Example 3 RE");
is (bAfterA_RE("bbb" ), true, "Example 4 RE");
done_testing;
}
sub runBenchmark($repeat)
{
use Benchmark qw/cmpthese/;
cmpthese( $repeat, {
index => sub{
bAfterA("aabb"), bAfterA("abab"), bAfterA("aaaa"), bAfterA("bbbb"),
},
regex => sub{
bAfterA_RE("aabb"), bAfterA_RE("abab"), bAfterA_RE("aaaa"), bAfterA_RE("bbbb"),
},
});
}
|