Write your own atoi ( ) | A simple Python3 program for implementation of atoi ; If whitespaces then ignore . ; Sign of number ; Checking for valid input ; Handling overflow test case ; Driver Code ; Functional Code
import sys NEW_LINE def myAtoi ( Str ) : NEW_LINE INDENT sign , base , i = 1 , 0 , 0 NEW_LINE while ( Str [ i ] == ' ▁ ' ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( Str [ i ] == ' - ' or Str [ i ] == ' + ' ) : NEW_LINE INDENT sign = 1 - 2 * ( Str [ i ] == ' - ' ) NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( Str ) and Str [ i ] >= '0' and Str [ i ] <= '9' ) : NEW_LINE INDENT if ( base > ( sys . maxsize // 10 ) or ( base == ( sys . maxsize // 10 ) and ( Str [ i ] - '0' ) > 7 ) ) : NEW_LINE INDENT if ( sign == 1 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT else : NEW_LINE INDENT return - ( sys . maxsize ) NEW_LINE DEDENT DEDENT base = 10 * base + ( ord ( Str [ i ] ) - ord ( '0' ) ) NEW_LINE i += 1 NEW_LINE DEDENT return base * sign NEW_LINE DEDENT Str = list ( " ▁ - 123" ) NEW_LINE val = myAtoi ( Str ) NEW_LINE print ( val ) NEW_LINE