From 9f9b8e2ad106bcce087969c94a9f6844f8900ff6 Mon Sep 17 00:00:00 2001 From: Gabriel Donnan <47415809+gabedonnan@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:48:28 +0000 Subject: [PATCH] Create custom-atoi.py Not the most efficient solution but a fine solution for a custom string to int conversion tool satisfying all constraints --- custom-atoi.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 custom-atoi.py diff --git a/custom-atoi.py b/custom-atoi.py new file mode 100644 index 0000000..1e1b755 --- /dev/null +++ b/custom-atoi.py @@ -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)] + +