Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Starter code: class Solution:
    def isNumber(self, s: str) -> bool:
        

Solutions:
class Solution:

	def isNumber(self, s):
		dot = False
		exp = False
		try:
			while s.startswith(' '):
				s = s[1:]
			while s.endswith(' '):
				s = s[:-1]
			if s.startswith('-') or s.startswith('+'):
				s = s[1:]
		except IndexError:
			return False
		if s == '':
			return False
		if s.startswith('e'):
			return False
		if (s[-1] > '9' or s[-1] < '0') and s[-1] != '.':
			return False
		if s.startswith('.'):
			if len(s) == 1:
				return False
			elif s[1] < '0' or s[1] > '9':
				return False
		i = 0
		while i < len(s):
			if s[i] < '0' or s[i] > '9':
				if s[i] == '.':
					if not dot and (not exp):
						dot = True
					else:
						return False
				elif s[i] == 'e':
					if not exp:
						exp = True
						if s[i + 1] == '-' or s[i + 1] == '+':
							i = i + 1
					else:
						return False
				else:
					return False
			i = i + 1
		return True


class Solution:

	def isNumber(self, s):
		num = dot = exp = sign = False
		numAfterE = True
		n = len(s)
		for i in range(len(s)):
			if s[i] == ' ':
				if i < n - 1 and s[i + 1] != ' ' and (num or dot or exp or sign):
					return False
			elif s[i] in '+-':
				if i > 0 and s[i - 1] != 'e' and (s[i - 1] != ' '):
					return False
				sign = True
			elif s[i].isdigit():
				num = True
				numAfterE = True
			elif s[i] == '.':
				if dot or exp:
					return False
				dot = True
			elif s[i] == 'e':
				if exp or not num:
					return False
				exp = True
				numAfterE = False
			else:
				return False
		return num and numAfterE


class Solution:

	def isNumber(self, s):
		try:
			float(s)
			return True
		except:
			return False


class Solution:

	def isNumber(self, s):
		states = [{}, {'blank': 1, 'sign': 2, 'dot': 4, 'digit': 3}, {'digit': 3, 'dot': 4}, {'digit': 3, 'blank': 9, 'e': 6, 'dot': 5}, {'digit': 5}, {'digit': 5, 'e': 6, 'blank': 9}, {'sign': 7, 'digit': 8}, {'digit': 8}, {'digit': 8, 'blank': 9}, {'blank': 9}]
		curr_state = 1
		for c in s:
			type_c = self.checkType(c)
			if type_c not in states[curr_state].keys():
				return False
			curr_state = states[curr_state][type_c]
		if curr_state not in [3, 5, 8, 9]:
			return False
		return True

	def checkType(self, c):
		if c == ' ':
			return 'blank'
		elif ord(c) >= ord('0') and ord(c) <= ord('9'):
			return 'digit'
		elif c == '.':
			return 'dot'
		elif c in ['E', 'e']:
			return 'e'
		elif c in ['+', '-']:
			return 'sign'
		else:
			return 'other'


class Solution:

	def isNumber(self, s):
		reg_sol = self.isNumber_regex(s)
		aut_sol = self.isNumber_automata(s)
		pyt_sol = self.isNumber_python(s)
		if not reg_sol == aut_sol == pyt_sol:
			raise Exception("'{s}' is {reg_sol} according to regex, {aut_sol} according to automata, {pyt_sol} according to float()".format(s=s, reg_sol=reg_sol, aut_sol=aut_sol, pyt_sol=pyt_sol))
		return aut_sol

	def isNumber_regex(self, s):
		import re
		pattern = re.compile('^ *[-+]?([0-9]*\\.[0-9]+|[0-9]+|[0-9]+\\.)([eE][-+]?([0-9]+))? *$')
		return None != pattern.match(s)
	transition = ({' ': 0, '.': 3, '-': 1, '0': 2}, {'.': 3, '0': 2}, {'0': 2, '.': 4, 'e': 5, ' ': 8}, {'0': 4}, {'0': 4, 'e': 5, ' ': 8}, {'-': 6, '0': 7}, {'0': 7}, {'0': 7, ' ': 8}, {' ': 8})
	final_states = (2, 4, 7, 8)

	def isNumber_automata(self, s):

		def key(char):
			if char in '\t':
				return ' '
			if char in '123456789':
				return '0'
			if char in 'E':
				return 'e'
			if char in '+':
				return '-'
			return char
		state = 0
		for i in s:
			try:
				state = self.transition[state][key(i)]
			except KeyError:
				return False
		return state in self.final_states

	def isNumber_python(self, s):
		try:
			f = float(s)
		except:
			return False
		else:
			return True

	def isNumber_manually(self, s):
		pass