-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_to_integer.py
46 lines (35 loc) · 1.07 KB
/
string_to_integer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
'''
first discard whitespace
then check if first non-white is valid character
then check for negative
then continue and contactenate to the string, to then convert to int
'''
int_str = ""
if not str:
return 0
i = 0
while str[i] == ' ':
i+=1
# now we're at where there's no whitespace left
negative = False
# check if valid conversion
if str[i] != '-' and int(str[i]) not in range(0, 10):
return 0
elif str[i] == '-':
negative = True
i += 1
for x in range(i, len(str)):
if int(x) not in range(10):
break
int_str += str[x]
print int_str
if negative:
return int(int_str) * -1
else:
return int(int_str)