17
2
Prelude:
I wanted to train making test cases, so I'm gonna try them on something easy.
Challenge:
Take any given (string) input (within visible ASCII range) and deduce if it's a number and output something that could be used to evaluate.
Rules:
- A number will only contain the characters
-0123456789,.
- Answers are only required to recognise numbers between -1000000000 and 1000000000 (exclusive), but may recognise arbitrarily large numbers.
- You can write a full program or a function.
- If a number, return anything that could be used to discern it and document the output in the description (ex.
My program outputs T if a number, F if not.
). - Input will be any amount of characters within ASCII range or empty (if empty return whatever you'd output if not a number).
- Numbers could include a decimal point (ex.
3.14
). If they do, they must have at least one digit before the decimal point and at least one after it. - Numbers could have leading or trailing zeros (ex.
000001.00000
). - The integer part of a number could be divided for readability into chunks of three digits with commas (ex.
1,000.23456
). In this case they must be divided every three digits from right to left (ex.1,234,567
,10,000.202
,123,234.00
,0,123.293
). - Negatives numbers are indicated by a leading
-
(ex.-1.23
). A leading+
to indicate a positive number is not permitted, and should result in the falsy output. - Exceptions are NOT counted as valid and discernible output (except when they can pass outputs to standard out stream (ex.
Exception on line N [...]
can be put in as output for a number/nonnumber if the string is put to standard out stream.
Test cases:
(assuming the My program outputs T if a number, F if not.
version)
123 -> T [on integer]
-123 -> T [negative numbers need to be handled]
0 -> T [on zero]
123.456 -> T [on floating point]
123,456.789 -> T [on evenly divided by 3 digits with comas]
123456789 -> T [thousand separators are not required]
0000001.00000 -> T [on leading/trailing zeros]
00.00 -> T [on zero and leading/trailing zeros]
999999999.9999999999999999999999999999999999999999999999999999 -> T [on close to the maximum value]
-999999999.9999999999999999999999999999999999999999999999999999 -> T [on close to the minimum value]
-> F [on empty]
lolz -> F [on non-number value]
n4melyh4xor -> F [on non-number with digits]
1.2 -> F [on space-padded]
9.3 1.3 -> F [on anyhow separated multiple numbers]
1e5 -> F [no scientific notation]
50cl05e.buty3ts0f4r -> F [on input with letters obscuring the number]
1,2,3,4.5678 -> F [on badly readability-divided number]
1,234.5,678 -> F [on readability-divided floating point part]
.234 -> F [on no leading zero]
+1 -> F [no leading + sign]
1.234.3 -> F [only one decimal point]
12345,678 -> F [on separator stopping mid-way]
code-golf, least characters is the winner.
Can we assume the input is always ASCII? – Brad Gilbert b2gills – 2016-12-17T17:04:54.160
@BradGilbertb2gills, yes. – n4melyh4xor – 2016-12-17T17:08:12.063
If
-123
is OK, what about+456
--> good or bad. Or is+
left out of the party? – chux - Reinstate Monica – 2016-12-17T17:39:22.467@chux, the party train left before the + got there.
Better luck next time, +. – n4melyh4xor – 2016-12-17T17:42:55.700
If the input is outside the range -1000000000 to 1000000000, should it return T or F? – Mitchell Spector – 2016-12-17T18:46:33.440
@MitchellSpector, if you are able to parse numbers bigger than that, feel free to. – n4melyh4xor – 2016-12-17T18:47:52.647
2When people ask for clarifications in comments, you should edit the question. It should never be necessary to read the comments in order to know the spec. I've edited to include the answers and some test cases. – Peter Taylor – 2016-12-17T23:04:39.237
"If a number, return anything that could be used to discern it and document the output in the description" - that's rather vague / poorly worded. If I output the input, it can still be used to discern whether the input was a number. – John Dvorak – 2016-12-18T15:51:49.387
A good test case would be one where the thousands separator stops mid-way.
12345,678 -> F
– John Dvorak – 2016-12-18T15:56:14.870@JanDvorak, cool, included. – n4melyh4xor – 2016-12-18T17:03:19.997
True ASCII is 7-bit, may I use PETSCII which is 8-bit ASCII-compatible character encoding ? – Shaun Bebbers – 2017-04-06T07:20:48.507