I am suppose to take in an input and return true or false depending on whether the input is a valid number. Here are some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3 " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false
I have a list of all of the valid characters and I decided to use slicing notation to see which character in the 0th index of the input matches any of the valid ones. However, the code is misreading the input 9
as false. This is my full code at the moment. I haven't yet checked for all conditions:
class Solution:
def isNumber(self, s: str) -> bool:
valid_char = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '+', 'e', '.']
str_lst = list(s)
if str_lst[0] in valid_char[0:10]:
return True
else:
return False