Well-Formed Byte Stream
A text encoding stores every character in 1 to 4 bytes:
- A character of 1 byte is a byte whose first bit is
0. - A character of 2, 3 or 4 bytes begins with a byte that starts with
110,1110or11110. Every byte after the first one starts with10.
data holds bytes as numbers from 0 to 255. Return True if data is a whole number of well-formed characters, one after another, otherwise False.
Example 1
data [226,130,172,65]
returns true
226 is 11100010, so two more bytes must start with 10: 130 and 172 do. 65 starts with 0 and stands alone.
Example 2
data [226,130,65]
returns false
226 asks for two bytes that start with 10, but 65 starts with 0.
Example 3
data [130]
returns false
130 is 10000010, and a byte that starts with 10 cannot begin a character.
Constraints
1 <= len(data) <= 600 <= data[i] <= 255
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.