Question: https://oj.leetcode.com/problems/valid-number/

Question Name: Valid Number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class Solution: # @param s, a string # @return, a boolean def isNumberWithoutE(self, s, decimalAllow = True): ''' Check whether s is a number not in scientific notation ''' digits = "1234567890" sLen = len(s) if sLen == 0: return False if s[0] == "+" or s[0] == "-": # Skip the sign s = s[1:]; sLen -= 1 if sLen == 0: return False if "." in s: if sLen == 1 or not decimalAllow: return False decimalPoint = s.index(".") for item in s[:decimalPoint] + s[decimalPoint+1:]: if not item in digits: return False else: # All elements are digits (except one decimal point) return True else: for item in s: if not item in digits: return False else: # All elements are digits return True # @param s, a string # @return a boolean def isNumber(self, s): ''' Check whether s is a valid number. ''' s = s.strip(" ") # Remove the leading and tailing spaces if "e" in s: # Might be a number in scientific notation. divPos = s.index("e") return self.isNumberWithoutE(s[:divPos]) and self.isNumberWithoutE(s[divPos+1:], False) elif "E" in s: # Might be a number in scientific notation divPos = s.index("E") return self.isNumberWithoutE(s[:divPos]) and self.isNumberWithoutE(s[divPos+1:], False) else: # Might be a number not in scientific notation return self.isNumberWithoutE(s) |

When I ran the code above on online judge, it says that “global name ‘decimalAllow’ is not defined”. I didn’t find a definition of this decimalAllow. Can you help explain it?

Thanks!

I tried the code and it works well.

The decimalAllow is a argument of the function:

def isNumberWithoutE(self, s, decimalAllow = True):

Hope it be helpful!