From ad6f251d62eaf4a4ad4a14152f75d4ea56c404f1 Mon Sep 17 00:00:00 2001 From: Mike Torii Date: Sun, 4 Sep 2022 22:23:59 +0900 Subject: [PATCH] Fixed errors when in string is just white space. --- Python/string-to-integer-atoi.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Python/string-to-integer-atoi.py b/Python/string-to-integer-atoi.py index b10431638..e1e8ea560 100644 --- a/Python/string-to-integer-atoi.py +++ b/Python/string-to-integer-atoi.py @@ -41,9 +41,9 @@ def atoi(self, str): i += 1 sign = 1 - if str[i] == "+": + if i < len(str) and str[i] == "+": i += 1 - elif str[i] == "-": + elif i < len(str) and str[i] == "-": sign = -1 i += 1 @@ -60,9 +60,9 @@ def atoi(self, str): return sign * result if __name__ == "__main__": - print Solution().atoi("") - print Solution().atoi("-1") - print Solution().atoi("2147483647") - print Solution().atoi("2147483648") - print Solution().atoi("-2147483648") - print Solution().atoi("-2147483649") \ No newline at end of file + print( Solution().atoi("") ) + print( Solution().atoi("-1") ) + print( Solution().atoi("2147483647") ) + print( Solution().atoi("2147483648") ) + print( Solution().atoi("-2147483648") ) + print( Solution().atoi("-2147483649") ) \ No newline at end of file