easy +10 pts

Parse Boolean Strings

Convert a messy boolean string into a Python bool, handling True/False/Yes/No/1/0 case-insensitively.

Write a function `parse_bool(value: str) -> bool` that converts a string representation of a boolean into a Python `bool`. The function should accept the following inputs (case-insensitive): "true", "false", "yes", "no", "1", "0". For any other input, raise a `ValueError` with the message `"Invalid boolean string: {value}"` (replace `{value}` with the actual input). The function must treat "true", "yes", "1" as `True`, and "false", "no", "0" as `False`.

Constraints

Input is a string. Length of string is between 1 and 20. The function must be case-insensitive. The function must raise `ValueError` for invalid inputs. Allowed time complexity: O(n), where n is the length of the string.

Example

>>> parse_bool("True")
True
>>> parse_bool("false")
False
>>> parse_bool("YES")
True
>>> parse_bool("0")
False
>>> try: parse_bool("maybe")
... except ValueError as e: print(e)
Invalid boolean string: maybe
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Convert the input string to lowercase to handle case-insensitivity.
Use a dictionary or set to map the accepted true and false values.
Remember to raise ValueError with the exact message format when the input is not recognized.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.