#!/usr/bin/env python3

__author__ = 'Wei Mu'

class Solution:
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        
        if str == '':
            return(0)
        
        negative = False
        
        for ind, char in enumerate(str):
            if char not in '1234567890-+ ':
                return(0)
            elif char == ' ':
                continue
            elif char == '-':
                negative = True
                ind+=1
                break
            elif char == '+':
                ind+=1
                break
            else: 
                break
        
        if ind > len(str) - 1:
            return(0)
        
        for ind2, char2 in enumerate(str[ind:]):
            if ind2 == len(str[ind:]) - 1 and str[-1] in '0123456789':
                ind2 +=1
            if char2 in '0123456789':
                continue
            else:
                break
        
        if ind2 == 0:
            return 0
        
        if negative == True:
            ans = -1 * int(str[ind:ind + ind2])
        else:
            ans = int(str[ind:ind + ind2])
        
        ans = min(ans, 2147483647)
        ans = max(ans, -2147483648)
        return ans
