blob: bda082bb9eadbb1afd0f548a986d253f2a69c217 (
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
|
"""
## Task 2: B After A
**Submitted by:** [Mohammad Sajid Anwar][0]
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
```
[0]: https://manwar.org/
"""
def b_after_a(string: str) -> bool:
"""
Returns `true` if there is at least one `b`, and no `a` appears after the
first `b`.
Args:
string: The input string to check for the presence of `b` and `a`.
Returns:
`True` if there is at least one `b`, and no `a` appears after the first
`b`; otherwise, `False`.
"""
if "b" not in string: # fails if there is no `b` in the string
return False
b_index = string.index("b")
# grab a slice of the string after the first `b`
sliced_after_b = string[b_index + 1:]
# check if there is no `a` in the slice
return "a" not in sliced_after_b
|