2
\$\begingroup\$

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 or 3.14
  • Complex: A complex number is a number that follows the format a+bj where a and b are numerical values. The plus sign can also be a minus sign e.g. 1+10j or 143-1j. If it saves bytes (or you want to), you may use i instead of j.
  • Longs: A long is any number that would be classed as an integer or a float, with an L at the end e.g 1325963L
  • 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 or afa not A5c
  • Scientific: A scientific number is a number which consists of a run of digits (1234567890.) followed by an e followed by a run of digits, optionally preceded by a -. A E 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 so shortest code, in bytes, wins!

\$\endgroup\$
9
  • \$\begingroup\$ Isn't 4E3 = 4000? \$\endgroup\$
    – Mr. Xcoder
    Jul 15, 2017 at 21:20
  • 7
    \$\begingroup\$ I don't downvote, but I find these rules (only lowercase e, j and not i, single L is OK) quite arbitrary. This reads basically "design me a regex for this spec". \$\endgroup\$
    – Uriel
    Jul 15, 2017 at 21:23
  • 1
    \$\begingroup\$ It seems exceedingly inconvenient to only allow the use of j rather than either i or j, so long as convention is internally consistent within the answer - as I would be willing to bet there is at least one language in which i is used to denote complex numbers - beyond this, I am aware of at least 20 languages in which E is the proper notation for scientific notation - by preventing this internal method of interpretation seems like it would artificially inflate some answers \$\endgroup\$ Jul 15, 2017 at 21:23
  • 1
    \$\begingroup\$ @Uriel I have changed it so that the rules are less rigid about input, and have changed the appropriate test cases. \$\endgroup\$ Jul 15, 2017 at 21:30
  • 1
    \$\begingroup\$ @TaylorScott it is. Read the part on floats again. \$\endgroup\$ Jul 15, 2017 at 21:42

3 Answers 3

4
\$\begingroup\$

SOGL V0.12, 53 52 50 bytes

O[►~hΘP█⁵\↔<sK⁹>¼⅝┘d⁾Δo≡7ξδ¾▓↑ΧGj4<eu╝t┼Pπ»‘øβ!d!-

Try it Here! or test all test cases (first number indicates amount of test cases)

based on a regex - ^(-?\d+((\.\d+|)(L|e-?\d+(\.\d+|)|\+\d+(\.\d+|)j|)|)|[A-F\d]+)$

\$\endgroup\$
2
  • \$\begingroup\$ Not sure this works for −3.5+2j or ab1 \$\endgroup\$
    – wrymug
    Jul 16, 2017 at 17:34
  • \$\begingroup\$ the first one uses a unicode dash, which shouldn't be (and has been fixed in the examples), and for the second - this expects all uppercase hexadecimal \$\endgroup\$
    – dzaima
    Jul 16, 2017 at 17:36
2
\$\begingroup\$

Javascript, 72 71 65 bytes

saved one byte thanks to @Cowsquack saved 6 bytes thanks to @dzaima

a=>/^(-?\d+((\.\d+|)(L|e-?\d+(\.\d+|)|\+\d+(\.\d+|)j|)|)|[A-F\d]+)$/.test(a)

JS port of @dzaima's answer. Credit to @dzaima for the regex.

\$\endgroup\$
10
  • \$\begingroup\$ /regex here/.test(a) is shorter than match \$\endgroup\$
    – user41805
    Jul 16, 2017 at 18:05
  • \$\begingroup\$ you don't need the ==null if you use test \$\endgroup\$
    – dzaima
    Jul 16, 2017 at 18:13
  • \$\begingroup\$ Fails for lowercase hexadecimals. \$\endgroup\$
    – darrylyeo
    Jul 16, 2017 at 18:15
  • \$\begingroup\$ @darrylyeo caird said in the comments that you can choose whether to support lowercase or uppercase hexadecimals \$\endgroup\$
    – dzaima
    Jul 16, 2017 at 18:16
  • \$\begingroup\$ Can you simply use /regex here/.test, eliminating a=> and (a)? I'm not a Javascript programmer, but this seems to me like it would work \$\endgroup\$ Jul 16, 2017 at 19:56
1
\$\begingroup\$

Python 3, 290 136 105 bytes

Python port of @dzaima's answer. All credit to @dzaima for the regex :)

lambda s:re.match('^(-?\d+((\.\d+|)(L|e-?\d+(\.\d+|)|\+\d+(\.\d+|)j|)|)|[A-F\d]+|[a-f\d]+)$',s)
import re

Try it online!

\$\endgroup\$
5
  • 2
    \$\begingroup\$ You don't need to account for both lowercase and uppercase hexadecimal (caird said that in the comments) \$\endgroup\$
    – dzaima
    Jul 16, 2017 at 17:45
  • 1
    \$\begingroup\$ You don't need to compile the regex. \$\endgroup\$ Jul 16, 2017 at 18:00
  • 1
    \$\begingroup\$ 111 bytes \$\endgroup\$ Jul 16, 2017 at 18:22
  • 1
    \$\begingroup\$ @totallyhuman thanks! I don't think it needs bool() though, because the re match object is truthy. \$\endgroup\$
    – wrymug
    Jul 16, 2017 at 18:28
  • 1
    \$\begingroup\$ It's 1 byte shorter as re.compile('...').match rather than lambda s:re.match('...',s) \$\endgroup\$ Jul 17, 2017 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.