diff options
| author | Ian Goodnight <ian.goodnight@scythe.io> | 2024-06-16 14:44:45 -0400 |
|---|---|---|
| committer | Ian Goodnight <ian.goodnight@scythe.io> | 2024-06-16 14:44:45 -0400 |
| commit | d87e83206ea21bc2290ce459ffba878e94e49f95 (patch) | |
| tree | 1cdd10ba3c58cf4925668c14a886e63041aa8634 /challenge-273/iangoodnight/python/task2.py | |
| parent | 194611acf9d0f7f165ac0ec595774b2c6fd5f413 (diff) | |
| download | perlweeklychallenge-club-d87e83206ea21bc2290ce459ffba878e94e49f95.tar.gz perlweeklychallenge-club-d87e83206ea21bc2290ce459ffba878e94e49f95.tar.bz2 perlweeklychallenge-club-d87e83206ea21bc2290ce459ffba878e94e49f95.zip | |
week 273
Diffstat (limited to 'challenge-273/iangoodnight/python/task2.py')
| -rw-r--r-- | challenge-273/iangoodnight/python/task2.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-273/iangoodnight/python/task2.py b/challenge-273/iangoodnight/python/task2.py new file mode 100644 index 0000000000..bda082bb9e --- /dev/null +++ b/challenge-273/iangoodnight/python/task2.py @@ -0,0 +1,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 |
