# Some problem with the test cases(Python).

• I use regular expression to solve this problem.And I can pass the test case on my own PC but not on the OJ. I don't know what is wrong with my code.Can someone help me?

Here is my code:

``````patternFirst = re.compile(r'^[+-]?[0]+\s+\d+')
matchFirst = patternFirst.match(string)
if matchFirst:
return 0
string = string.replace(' ', '')
pattern = re.compile(r'^[+-]?\d+[a-zA-Z0-9]*\$')
match = pattern.match(string)
if match:
patternAgain = re.compile(r'^[\+\-]?\d+')
matchAgain = patternAgain.match(match.group())
return int(matchAgain.group())
else:
return 0
``````

And test cases is '+0 123'.

• You didn't do any overflow testing. You need to be aware of the range of int 32.

• In this problem, if there is should be no space between the digit. So in the test case, '+0 123', the result should be 0, instead of 123. Hence you should try to code this way.
(Since your code `string = string.replace(' ', '')` replaced all inner spaces, then the result of your code should be 123).
The above explained why you failed in the test case '+0 123'. But you still need to improve your code to deal with other problems ^_^.

I don't have time to write in python, but u can consider the java code below:

``````public class Atoi {
public int atoi(String str) {
if (str == null || str.length() == 0) return 0;
int i = 0;
while(i< str.length() && str.charAt(i) == ' ') i++;
int sign = 1;
long result = 0;
if(i<str.length()){
if(str.charAt(i) == '+' || str.charAt(i) == '-'){
if(str.charAt(i) == '-') sign = -1;
i++;
}
while(i<str.length()){
if(isDigit(str.charAt(i)))
{
result = result * 10 + (str.charAt(i)-'0');
if(result >= Integer.MAX_VALUE){
break;
}
}
/*else if(str.charAt(i) == ' ') {
//if we see a space, we should break ^_^
}*/
else{
break;
}
i++;
}
}
result = result * sign;
if(result >= Integer.MAX_VALUE) return Integer.MAX_VALUE;
if(result <= Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) result;
}

public boolean isDigit(char c){
return c >='0' && c<='9';
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.