aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/iangoodnight/perl/task2.pm
blob: 14ee6b18decb44e874120579e9890d6ba29e6aab (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl

=begin comment

## Task 2: B After A

**Submitted by:** [Mohammad Sajid Anwar][2]

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
```

[2]: https://manwar.org/

=end comment
=cut

use strict;
use warnings;
use autodie;
use Exporter;

our $VERSION = '1.0.0';

our @EXPORT_OK = qw(b_after_a);

sub b_after_a {
    my $str = shift // q{};

    # Regular expression to match the string
    ## no critic (RegularExpressions::RequireDotMatchAnything)
    my $re = qr{
        ^      # Start of the string
        [^b]*  # Match any character except 'b' zero or more times
        b+     # Match 'b' one or more times
        [^a]*  # Match any character except 'a' zero or more times
        $      # End of the string
    }mx;
    ## use critic
    # Test the string against the regular expression and return the result
    if ( $str =~ $re ) {
        return 1;
    }
    return 0;
}

1;

__END__

=head1 NAME

Task2: B After A

=head1 VERSION

This documentation refers to Task2 version 1.0.0

=head1 EXPORTS

The function L</b_after_a> is exported by default.

=head1 USAGE

    use strict;
    use warnings;
    use feature qw(say);
    use Task2 qw(b_after_a);

    my $str1 = 'aabb';
    my $str2 = 'abab';

    say b_after_a($str1); # Output: 1
    say b_after_a($str2); # Output: 0

=head1 DESCRIPTION

Given a string, C<$str>, the function C<b_after_a> returns true if there is at
least one 'b', and no 'a' appears after the first 'b'.

=head1 REQUIRED ARGUMENTS

=over 4

=item C<$str>

A string.

=back

=head1 OPTIONS

None.

=head1 DIAGNOSTICS

None.

=head1 EXIT STATUS

None.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

None.

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

None reported.

=head1 AUTHOR

Ian Goodnight

=head1 LICENSE AND COPYRIGHT

This software is released under the terms of theGNU General Public License
Version 3.

=cut