Some kind of bug with the "+ 1" test case


  • 0
    N

    Hi there,

    Here is my solution:

    def isNumber(s):
    try:
                int(s)
                return True
            except:
                pass
            try:
                float(s)
                return True
            except:
                pass
            if not any([x.isnumeric() for x in s]):
                return False
            if not s[0].isnumeric() or not s[-1].isnumeric():
                return False
            pre_e = True
            pre_e_dot = False
            post_e_dot = False
            for char in s:
                if not char.isnumeric():
                    if char == "e":
                        if not pre_e:
                            pre_e = False
                            continue
                        else:
                            return False
                    if pre_e and char == ".":
                        if not pre_e_dot:
                            pre_e_dot = True
                            continue
                        else:
                            return False
                    if not pre_e and char == ".":
                        if not post_e_dot:
                            post_e_dot = True
                            continue
                        else:
                            return False
                    return False
            return True
    

    But for some reason when I run my code I fail on only "+ 1". However, I just tried my solution offline and it handles that case fine. I'm wondering if anyone else has ever experienced one of these kinds of issues. To get my final submission to be accepted I had to specifically check if it was that one test case.


  • 0

    It's because offline you used Python 3 which throws an exception for int('+ 1') and LeetCode uses Python 2 which happily turns that into 1.


Log in to reply
 

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