-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToInteger.py
More file actions
56 lines (48 loc) · 1.55 KB
/
Copy pathStringToInteger.py
File metadata and controls
56 lines (48 loc) · 1.55 KB
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):
result = ''
sign = ''
for i in s:
if i == "+" or i == "-":
if not sign and not result :
sign = i
else:
if result:
result = sign + result
num = int(result)
if num < (-2 ** 31):
return -2 ** 31
elif num > (2 ** 31) - 1:
return 2 ** 31 - 1
else:
return num
else:
return 0
elif i.isnumeric():
result = result + i
elif i == " " and not result and not sign:
result = result
else:
if result:
result = sign + result
num = int(result)
if num < (-2 ** 31):
return -2 ** 31
elif num > (2 ** 31) - 1:
return 2 ** 31 - 1
else:
return num
else:
return 0
if result:
result = sign + result
num = int(result)
if num < (-2 ** 31):
return -2 ** 31
elif num > (2 ** 31) - 1:
return 2 ** 31 - 1
else:
return num
else:
return 0
s = input("Enter a string: ")
print(myAtoi(s))