blob: fcf2ad08c2cfbba09ea169d7a02f4bf8eacda848 (
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
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
|
#!/usr/bin/env perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/#TASK2
#
# 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
#
############################################################
##
## discussion
##
############################################################
#
# We walk the string character for character. If we find a "b"
# we remember this fact. If we encounter an "a" and have already
# seen a "b", we return false. If we're at the end of the string
# our output depends on whether or not we have seen a "b": we
# didn't bail out for seeing an "a" after a "b", so if we didn't
# see any "b" at all, we return false, otherwise true.
use strict;
use warnings;
b_after_a("aabb");
b_after_a("abab");
b_after_a("aaa");
b_after_a("bbb");
sub b_after_a {
my $str = shift;
print "Input: '$str'\n";
my $b_seen = 0;
foreach my $char (split //, $str) {
$b_seen = 1 if $char eq "b";
if($b_seen) {
return print "Output: false\n" if $char eq "a";
}
}
if($b_seen) {
print "Output: true\n";
} else {
print "Output: false\n";
}
}
|