From d87e83206ea21bc2290ce459ffba878e94e49f95 Mon Sep 17 00:00:00 2001 From: Ian Goodnight Date: Sun, 16 Jun 2024 14:44:45 -0400 Subject: week 273 --- challenge-273/iangoodnight/python/task2.py | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 challenge-273/iangoodnight/python/task2.py (limited to 'challenge-273/iangoodnight/python/task2.py') 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 -- cgit