prize18-3 Exercise 1: Flip Case with One Bit ============================================= Idea: in ASCII, "A" is 0100 0001 and "a" is 0110 0001. They differ only in bit 5 (value 0x20), so XOR with 0x20 toggles case. It must only be applied to ASCII letters. XOR on other characters would corrupt them (for example it would turn a digit or a space into something else), and an accented letter such as e-acute is not in ASCII, so its code point is left alone. ex1.py: def flip_case(s): return "".join(chr(ord(c) ^ 0x20) if c.isascii() and c.isalpha() else c for c in s) print(flip_case("Hello, World 42!")) ok = all(flip_case(chr(c)) == chr(c).swapcase() for c in range(128)) print("matches str.swapcase on all of ASCII:", ok) print(flip_case("caf\u00e9")) Real output (Python 3.14): hELLO, wORLD 42! matches str.swapcase on all of ASCII: True CAFĂ© The e-acute is untouched because the test c.isascii() is false for it. Its case rule is not "one bit": e-acute is U+00E9 and its capital is U+00C9, which happens to differ by 0x20 as well here, but that is a Latin-1 coincidence, not a general rule, and other scripts do not follow it.