Inspired (like 2 others) by this chat message. (and to some extent, my CMC)
Given a string as an input, your task is to check whether or not that string is a number.
What is a number?
This is a list of all input formats that are "numbers":
- Integers: An integer is a string made entirely of characters from
1234567890
, without any other character e.g.152
- Floats: A float is a integer with 1 (no more, no less) decimal points (
.
) in the middle of the number. A decimal point must always be preceded and followed by a number e.g.1.0
or3.14
- Complex: A complex number is a number that follows the format
a+bj
wherea
andb
are numerical values. The plus sign can also be a minus sign e.g.1+10j
or143-1j
. If it saves bytes (or you want to), you may usei
instead ofj
. - Longs: A long is any number that would be classed as an integer or a float, with an
L
at the end e.g1325963L
- Hexadecimal: A hexadecimal number is a number which consists entirely of characters in
1234567890abcdef
. Upper case characters are valid hex, but only when used consistently e.g.10a
orafa
notA5c
- Scientific: A scientific number is a number which consists of a run of digits (
1234567890.
) followed by ane
followed by a run of digits, optionally preceded by a-
. AE
may also be used.
Any of these 5 may also be preceded by a minus sign (-
) but any whitespace in the number causes it to no longer be a number e.g. -3.5 + 2i
isn't a number. Longs may not be hexadecimal but may be floats
You must output 2 distinct values (preferably truthy and falsy but you may choose) for inputs that are either numbers as defined above, or not.
Test Cases
These should result in truthy
10
-9.34
-3.5+2j
ab1
100000L
3.14159L
-73e-7
7E53C80
1.2e3
12E3.4
And these should be falsy
abcdefghijklmnop
10.10.10
2569LL
10L+9j
8+45io
4aE3
3.
.3
Now because small numbers are easier to work with, this is a code-golf so shortest code, in bytes, wins!
4E3
= 4000? \$\endgroup\$e
,j
and noti
, singleL
is OK) quite arbitrary. This reads basically "design me a regex for this spec". \$\endgroup\$j
rather than eitheri
orj
, so long as convention is internally consistent within the answer - as I would be willing to bet there is at least one language in whichi
is used to denote complex numbers - beyond this, I am aware of at least 20 languages in whichE
is the proper notation for scientific notation - by preventing this internal method of interpretation seems like it would artificially inflate some answers \$\endgroup\$