Skip to content

Commit

Permalink
Create custom-atoi.py
Browse files Browse the repository at this point in the history
Not the most efficient solution but a fine solution for a custom string to int conversion tool satisfying all constraints
  • Loading branch information
gabedonnan authored Jan 12, 2023
1 parent d8582e9 commit 9f9b8e2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions custom-atoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution(object):
def myAtoi(self, s):
"""
:type s: str
:rtype: int
"""
start = 99999
flipper = 1
temp = ""
final = 0
for i,char in enumerate(s):
if i < start and char == "-":
flipper = -1
start = i
elif i < start and char == "+":
flipper = 1
start = i
elif i < start and char in "1234567890":
temp += char
start = i
elif i < start and char != " ":
return 0
elif i > start and char not in "1234567890":
break
else:
temp += char
length = len(temp)
#print(temp)
for j in range(length):
final += 10**(length-j-1) * self.chtoi(temp[j])
#print(self.chtoi(temp[j]))
if flipper == 1:
final = min(final, 2147483647)
else:
final = min(final, 2147483648)
return final * flipper

def chtoi(self, ch):
numbers = "9876543210"
realnums = [9,8,7,6,5,4,3,2,1,0]
return realnums[numbers.find(ch)]


0 comments on commit 9f9b8e2

Please sign in to comment.