easy +6 pts

Toggle a boolean n times

Flip a boolean value exactly n times and return the final state.

Write a function `toggle_bool(value: bool, n: int) -> bool` that returns the result of toggling `value` exactly `n` times. Toggling means: `True` becomes `False`, and `False` becomes `True`. `n` is a non-negative integer. If `n` is 0, the value is unchanged. You may assume `value` is always a boolean. Implement the function in Python. Do not read input or print output.

Constraints

- `value` is a boolean (`True` or `False`) - `0 <= n <= 10^9` - Return a boolean. - The solution should not use a loop with O(n) time.

Example

>>> bool(toggle_bool(True, 0))
True
>>> bool(toggle_bool(True, 1))
False
>>> bool(toggle_bool(False, 2))
False
>>> bool(toggle_bool(True, 3))
False
6 points ~8 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Toggling twice returns the original value.
Consider the parity of n.
You can use the modulo operator or bitwise XOR.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.