Skip to content

Commit 93492bf

Browse files
committed
programming qstn 8 soln added
1 parent 53146f5 commit 93492bf

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

programming-py/8.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import re
2+
3+
4+
def myAtoi(s):
5+
filtered_string = ""
6+
till_now = ""
7+
for each in s:
8+
if each == ".":
9+
break
10+
if re.match("[0-9-+]", each):
11+
if not re.search("[a-zA-Z]+", till_now):
12+
if (filtered_string != "" and re.search(r"\s$", till_now)) or ((each == "-" or each == "+") and re.search("[-+0-9]+", filtered_string)):
13+
break
14+
filtered_string += each
15+
till_now += each
16+
try:
17+
numb = int(filtered_string)
18+
if numb < -2147483648:
19+
return -2147483648
20+
elif numb >= 2147483648:
21+
return 2147483647
22+
else:
23+
return numb
24+
except:
25+
return 0
26+
27+
28+
# test below
29+
print(myAtoi("42"))
30+
print(myAtoi(" -42"))
31+
print(myAtoi("words and 987"))
32+
print(myAtoi("-91283472332"))
33+
print(myAtoi("0032"))
34+
print(myAtoi("3.14159"))
35+
print(myAtoi(" +0 123"))
36+
print(myAtoi("-5-"))
37+
print(myAtoi("-13+8"))
38+
print(myAtoi("123-"))

0 commit comments

Comments
 (0)