Parse a Number
Parsers often need to turn user input into a number, even when the text has spaces or extra characters after the number. Write a function my_atoi that behaves like atoi: skip leading spaces, read an optional sign, then read digits until the first non-digit character. Return the parsed integer. If it goes outside the 32-bit signed integer range [-2**31, 2**31 - 1], clamp it to the nearest boundary.
Примеры
Ввод
"42"Вывод
42Ввод
" -42"Вывод
-42Ввод
"4193 with words"Вывод
4193Нужно реализовать
def my_atoi(s: str) -> int:
Теги
stringsmath
Log in om je inzendingsgeschiedenis te zien
InloggenLog in om de AI-mentor te gebruiken
InloggenVisible tests
1
Input
"42"Expected
422
Input
" -42"Expected
-423
Input
"4193 with words"Expected
41934
Input
"words and 987"Expected
05
Input
"-91283472332"Expected
-21474836486
Input
"+1"Expected
1Focus radio
Paused · SomaFM · Fluid