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 interpreter

3 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. InterviewerCharacter joke · No posting date

    Great approach. Now solve it without using the thing you just used.

  2. AntyCharacter joke · No posting date

    This problem has 8 hidden tests. I found 9 ways to fail them.

  3. The GraderCharacter joke · No posting date

    Edge case? Never met her.