-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
56 lines (52 loc) · 1.27 KB
/
8.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
47
48
49
50
51
52
53
54
55
56
def myAtoi(s):
if len(s)==0:
return 0
sign='positive'
sign_found=False
isfloat=False
for i in range(len(s)):
if s[i]=='.':
isfloat=True
if isfloat==True:
s=float(s)
s=int(s)
s=str(s)
i=0
while not s[i].isdigit():
if sign_found==True:
return 0
if s[i]=='-' and sign_found==False:
sign='negative'
sign_found=True
s=s.replace(s[i],"",1)
elif s[i]=='+' and sign_found==False:
sign='positive'
sign_found=True
s=s.replace(s[i],"",1)
elif s[i]==' ':
s=s.replace(s[i],"",1)
else:
return 0
if len(s)==0:
return 0
""" i=len(s)-1
while not s[i].isdigit():
i-=1 """
string=''
i=0
while i<len(s):
if s[i].isdigit():
string+=s[i]
i+=1
else:
break
"""for z in range(i+1):
string+=s[z] """
number=int(string)
if sign=='negative':
number*=-1
if number<-2**31:
number=-2**31
elif number>2**31 - 1:
number=2**31-1
return number