String to Integer (atoi)
medium · parsing
Parse text into a signed 32-bit integer. Skip leading ASCII spaces only (' '), read at most one optional '+' or '-', then consume consecutive ASCII digits '0'..'9'. Stop at the first other character. If no digits were consumed, return 0. Clamp to [-2147483648,2147483647]. Do not treat tabs, Unicode digits, decimal points or internal spaces as digits. At most 200 characters.
Examples (JSON; null means Python None):
Input: {"text": " -42 words"}
Output: -42
Input: {"text": "91283472332"}
Output: 2147483647
Starter code
def parse_integer(text: str) -> int:
"""Parse an ASCII integer prefix and clamp its signed value."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler