# What is the error in code? This seems to run fine in my IDE but doesn't work on online terminal

2

I'm trying to solve the leetcode question 8 String to integer(atoi). But I'm getting a Runtime Error.

TypeError: 'str' object is not callable Line 9 in myAtoi (Solution.py)

I understand it's due to a string object getting called but can't seem to figure out what to fix.

``````class Solution:
def myAtoi(self, str: str) -> int:
string = str.strip()
if string=='-' or string=='+' or string.isdigit():
new = string
for i in range(1,len(string)):
if string[i].isdigit():
new = new+string[i]
if i!=len(string)-1 and (string[i+1]==' ' or string(i+1).isalpha()):
break
if(int(new)>=0):
if((int(new) & 0x7fffffff)==int(new)):
return int(new)
else:
return int(0x7fffffff)
else:
if((int(new) & -0x80000000)==int(-0x80000000)):
return int(new)
else:
return int(-0x80000000)
``````

Can anyone please tell me what to fix here?

python
python-3.x
string
typeerror

1

You don't return anything in the first `if` and you have an error in string calling, you use `()` instead of `[]`, the correct code will be:

``````class Solution:
def myAtoi(self, str: str) -> int:
string = str.strip()
if string=='-' or string=='+' or string.isdigit():
new = string
for i in range(1,len(string)):
if string[i].isdigit():
new = new+string[i]
if i!=len(string)-1 and (string[i+1]==' ' or string[i+1].isalpha()):
break
if(int(new)>=0):
if((int(new) & 0x7fffffff)==int(new)):
return int(new)
else:
return int(0x7fffffff)
else:
if((int(new) & -0x80000000)==int(-0x80000000)):
return int(new)
else:
return int(-0x80000000)
else:
return 0

print(Solution().myAtoi("abc"))
print(Solution().myAtoi("ab"))
print(Solution().myAtoi("a"))
print(Solution().myAtoi("abc d e fgh"))
print(Solution().myAtoi("452 abc d e fgh"))
print(Solution().myAtoi("abc d e fgh 452"))
print(Solution().myAtoi("   -42"))
``````
0

For anyone who's wondering the final answer to this question, it is:

``````class Solution:
def myAtoi(self, str: str) -> int:
string = str.strip()
if not string:
return 0
if not string.strip("+-"):
return 0
if string=='-' or string=='+' or string.isdigit():
new = string
for i in range(1,len(string)):
if string[i].isdigit() or string[i]=='.':
new = new+string[i]
if i!=len(string)-1 and (string[i+1]==' ' or string[i+1].isalpha()):
break
elif string[i]=='-' or string[i]=='+':
break
elif string[i].isalpha():
break
else:
return 0
new2 = new.strip("+-")
if new2:
if(int(float(new))>=0):
if((int(float(new)) & 0x7fffffff)==int(float(new))):
return int(float(new))
else:
return int(0x7fffffff)
else:
if((int(float(new)) & -0x80000000)==int(-0x80000000)):
return int(float(new))
else:
return int(-0x80000000)
else:
return 0
else:
return 0
``````

Is this the optimal solution? No! But is the best I could come up with.

Runtime: 44 ms, faster than 47.19% of Python3 online submissions for String to Integer (atoi). Memory Usage: 13.9 MB, less than 5.95% of Python3 online submissions for String to Integer (atoi).

User contributions licensed under CC BY-SA 3.0