```
from functools import reduce
import re
class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} #Dict for character to number
negative = 0 #Positive if 0 else negative
str = str.lstrip() #Remove the left spaces of the string
if not str: return 0 #Return 0 if the string is None
# If the first character is '-', put the negative = 1 and slice the string
if str[0] == '-':
negative = 1
str = str[1:]
# Else if the first character is '+', just slice the string
elif str[0] == '+':
str = str[1:]
# If the first character exists and is a number, try to get the number using regular expression
if str and str[0].isnumeric():
m = re.match(r'(\d+)', str)
str = m.group(1)
# If not, return 0
else:
return 0
# Get each number with corresponding character from the DIGITS dict and then calculate the final number with reduce()
n = reduce(lambda x, y: x * 10 + y, map(lambda x: DIGITS[x], str))
if n > 2147483647 and negative == 0:
n = 2147483647
elif n > 2147483648 and negative == 1:
n = 2147483648
if negative == 1:
n = -n
return n
```