# Determine the type of an input

The challenge is simple: Determine the type of an input, and output an identifier telling what type it is.

• "UI", Unsigned integer: 0, 1,34, 111111111111111111111111111111111111111111
• "SI", Signed integer: +0, +1, +42, -1, -3, -111111111111111111111111111111111111111111
• "UD", Unsigned decimal: 0.0, 1.23, 1234.1234
• "SD", Signed decimal: -0.0, +0.0, -1.235
• "LE", Letter: a-z and A-Z
• "SY", Symbol: ASCII code points: [32-47, 58-64, 91-96, 123-126] (i.e. all characters except numbers and letters)
• "ST", String: Two or more character that can't be parsed as any of the above number formats

Rules:

• The input will be 1-99 characters long
• The input will only contain printable ASCII characters, code points: 32-126.
• The output should be the two identifier letters as defined above (UI, SI ...)
• Standard I/O rules apply

Examples:

UI:
0
01
34
12938219383278319086135768712319838871631827319218923

SI:
-0
+01
+1
-123
+123

UD:
0.0
3.1415
2.718281828459045235360287471352662497757

SD:
+0.0
-3.1415
+2.718281828459045235360287471352662497757

LE:
a
k
L
Z

SY:
@
"
+
-

ST:
Hello, World!
f2!"
+23df
1234A
'"!
.012
1.
UI
+-1
5+3

• Can SY be more than one character? – FryAmTheEggman Mar 31 '16 at 15:27
• I would not consider 111111111111111111111111111111111111111111 to be of integer type. – Matt Mar 31 '16 at 16:31
• @Matt, It might not be a uint8 or int64, but it's definitely an integer. – Stewie Griffin Mar 31 '16 at 21:35
• @RosLuP it's not meant to be the type your language interprets it as. You should simply output two characters based on the input string/number. – Stewie Griffin Nov 28 '17 at 20:43
• @RosLuP I'm not sure you get the point of this challenge. It doesn't matter how it's interpreted in some language, or what the normal naming convention for numbers is. In this challenge "signed" is defined as: "There's a plus or minus sign followed by a number". You may disregard the words signed and unsigned doesn't actually affect the challenge, you should output a two character string based on some input. – Stewie Griffin Nov 29 '17 at 13:22

# Pyth - 47 bytes

Can prolly golf a few bytes off with some slicing tricks.

.x-+?@z"+-"\S\U?@z\.\D\Isz?!tz?}rzZG"LE""SY""ST


# JavaScript (ES6), 99

x=>(z=x.match(/^([+-])?\d+(\.\d+)?$/))?'SU'[+!z[1]]+'DI'[+!z[2]]:x[1]?'ST':parseInt(x,36)?'LE':'SY'  Test f=x=>(z=x.match(/^([+-])?\d+(\.\d+)?$/))?'SU'[+!z[1]]+'DI'[+!z[2]]:x[1]?'ST':parseInt(x,36)?'LE':'SY'

console.log=x=>O.textContent+=x+'\n'

;console.log(['0','01','34','12938219383278319086135768712319838871631827319218923'].map(x=>f(x)+' '+x).join\n)
;console.log(['-0','+01','+1','-123','+123'].map(x=>f(x)+' '+x).join\n)
;console.log(['0.0','3.1415','2.718281828459045235360287471352662497757'].map(x=>f(x)+' '+x).join\n)
;console.log(['+0.0','-3.1415','+2.718281828459045235360287471352662497757'].map(x=>f(x)+' '+x).join\n)
;console.log([...'akLZ'].map(x=>f(x)+' '+x).join\n)
;console.log([...'@"+-'].map(x=>f(x)+' '+x).join\n)
;console.log(['Hello, World!','f2!"','+23df','1234A','"!,'.012','1.','UI','+-1','5+3'].map(x=>f(x)+' '+x).join\n)
<pre id=O></pre>

• I was pretty sure that someone would be able to optimise for shared cases but I like your use of parseInt to detect letters. – Neil Apr 1 '16 at 0:13
• The first ( ) in your regex is not necessary – Awashi Apr 1 '16 at 7:25
• @Awashi it is necessary as I need a capture group for the sign to differentiate S or U. – edc65 Apr 1 '16 at 7:57
• @WashingtonGuedes no, it could be (\+|-) but no bytes saved – edc65 Apr 2 '16 at 8:31

# Turing Machine Code, 1544 bytes

Try it online!

0 + _ r s
0 - _ r s
0 0 _ r u
0 1 _ r u
0 2 _ r u
0 3 _ r u
0 4 _ r u
0 5 _ r u
0 6 _ r u
0 7 _ r u
0 8 _ r u
0 9 _ r u
0 a _ r l
0 b _ r l
0 c _ r l
0 d _ r l
0 e _ r l
0 f _ r l
0 g _ r l
0 h _ r l
0 i _ r l
0 j _ r l
0 k _ r l
0 l _ r l
0 m _ r l
0 n _ r l
0 o _ r l
0 p _ r l
0 q _ r l
0 r _ r l
0 s _ r l
0 t _ r l
0 u _ r l
0 v _ r l
0 w _ r l
0 x _ r l
0 y _ r l
0 z _ r l
0 A _ r l
0 B _ r l
0 C _ r l
0 D _ r l
0 E _ r l
0 F _ r l
0 G _ r l
0 H _ r l
0 I _ r l
0 J _ r l
0 K _ r l
0 L _ r l
0 M _ r l
0 N _ r l
0 O _ r l
0 P _ r l
0 Q _ r l
0 R _ r l
0 S _ r l
0 T _ r l
0 U _ r l
0 V _ r l
0 W _ r l
0 X _ r l
0 Y _ r l
0 Z _ r l
0 * _ r y
s 0 _ r s
s 1 _ r s
s 2 _ r s
s 3 _ r s
s 4 _ r s
s 5 _ r s
s 6 _ r s
s 7 _ r s
s 8 _ r s
s 9 _ r s
s . _ r d
s _ _ r i
s * _ r T
u 0 _ r u
u 1 _ r u
u 2 _ r u
u 3 _ r u
u 4 _ r u
u 5 _ r u
u 6 _ r u
u 7 _ r u
u 8 _ r u
u 9 _ r u
u . _ r D
u _ _ r I
u * _ r T
l _ _ r L
l * _ r T
y _ _ r S
y * _ r T
d 0 _ r d
d 1 _ r d
d 2 _ r d
d 3 _ r d
d 4 _ r d
d 5 _ r d
d 6 _ r d
d 7 _ r d
d 8 _ r d
d 9 _ r d
d _ _ r e
d * _ r T
i 0 _ r i
i 1 _ r i
i 2 _ r i
i 3 _ r i
i 4 _ r i
i 5 _ r i
i 6 _ r i
i 7 _ r i
i 8 _ r i
i 9 _ r i
i _ _ r j
i * _ r T
D 0 _ r D
D 1 _ r D
D 2 _ r D
D 3 _ r D
D 4 _ r D
D 5 _ r D
D 6 _ r D
D 7 _ r D
D 8 _ r D
D 9 _ r D
D _ _ r E
D * _ r T
I 0 _ r I
I 1 _ r I
I 2 _ r I
I 3 _ r I
I 4 _ r I
I 5 _ r I
I 6 _ r I
I 7 _ r I
I 8 _ r I
I 9 _ r I
I _ _ r J
I * _ r T
L * L r M
M * E r halt
S * S r Y
Y * Y r halt
e * S r f
f * D r halt
j * S r k
k * I r halt
E * U r f
J * U r k
T _ S r U
T * _ r T
U * T r halt

• Please include a fully-golfed version of the code. If the whitespace cannot be removed, it must be counted in the byte count. – user45941 Mar 31 '16 at 20:33
• Looks like it can not be removed, the program does not work properly without spaces between the symbols. – Matthew Smith Mar 31 '16 at 20:41
• It doesn't seem to classify "+-1" (from the examples) as a ST. – Xantix Apr 1 '16 at 1:05

# Retina, 98 97 bytes

A nice way to practise my regex skills indeed.

Try it online!

^(?![+-]?\d+(\.\d+)?$)..+ ST ^([+-]?)\d+$
$1UI ^([+-]?)\d+\.\d+$
$1UD i^[a-z]$
LE
^.$SY [+-]U S  • You can change ^[a-zA-Z]$ to i^[a-z]$ to save one byte – daavko Mar 31 '16 at 17:01 # Lua, 157 bytes Try it online! Golfed: n=(...)m=string.match s=m(n,"^[+-]")and"S"or"U"print(m(n,"^[+-]?%d+%.%d+$")and s.."D"or m(n,"^[+-]?%d+")and s.."I"or m(n,"^%w$")and"LE"or#n==1 and"SY"or"ST")  Ungolfed: n = "2.718281828459045" s = n:sub(1,1):match("[+-]") and "S" or "U" if n:match("^[+-]?%d+%.%d+$") then
print(s.."D")
elseif n:match("^[+-]?%d+") then
print(s.."I")
elseif n:match("^%w$") then print("LE") elseif #n==1 then print("SY") else print("ST") end  ## JavaScript (ES6), 125 120 bytes s=>"UISIUDSDLESYST".substr(s.match(/^((\d+)|([+-]\d+)|(\d+\.\d+)|([+-]\d+\.\d+)|([A-Z])|(.)|(.*))$/i).indexOf(s,2)*2-4,2)


Alternative version, also 120 bytes:

s=>"STUISIUDSDLESY".substr(s.match(/^(?:(\d+)|([+-]\d+)|(\d+\.\d+)|([+-]\d+\.\d+)|([A-Z])|(.)|.*)$/i).lastIndexOf(s)*2,2)  • Surely the regex can be golfed with a regex eval(/regex/) constructor + template strings – Downgoat Mar 31 '16 at 22:47 # Java, 192 bytes String t(String v){for(String[]x:new String[][]{{"\\d+","UI"},{"[-+]\\d+","SI"},{"\\d+\\.\\d+","UD"},{"[-+]\\d+\\.\\d+","SD"}})if(v.matches(x[0]))return x[1];return (v.length()==1?"SY":"ST");}  • return (v.length()==1?"SY":"ST"); can be return v.length()<2?"SY":"ST"; (-3 bytes) Or it can be this: String t(String v){for(String x:"UI\\d+;SI[-+]\\d+;UD\\d+\\.\\d+;SD[-+]\\d+\\.\\d+".split(";"))if(v.matches(x.substring(2)))return x.substring(0,2);return v.length()<2?"SY":"ST";} (179 bytes) And in addition you could change String t(String v) to v-> when you use a Java 8 lambda. – Kevin Cruijssen Nov 16 '17 at 12:34 # Javascript (ES6), 138 bytes I tried to use a replace to be more "fancy". This creates an anonymous function that returns the string of the type. s=>s.replace(/^((([+-])?(\d+)(\.\d+)?)|([a-z])|([ -~])|([^\0]*))$/i,(_,a,b,c,d,e,f,g)=>b?(c?'S':'U')+(e?'D':'I'):(f?'LE':'S'+(g?'Y':'T')))


Any tips to improving this will be entirely welcome.

• 1. should be ST, not UD. Change your \d* to \d+ – edc65 Mar 31 '16 at 23:23
• @edc65 How? That's a decimal. That is the same as 1.0. – Ismael Miguel Mar 31 '16 at 23:24
• It could be valid or not (I don't write 1. instead of 1) BUT It's not your choice or mine: there are the test cases – edc65 Mar 31 '16 at 23:26
• @edc65 You're right. I skipped the VERY huge list. I've fixed it. Thanks! – Ismael Miguel Mar 31 '16 at 23:26

# Python 3.5 - 241 240 bytes:

(Saved 1 byte thanks to @CatsAreFluffy)

import re
def r(g):
y={'^\d+$':'UI','^[+-]\d+$':'SI','^[0-9]\d*(\.\d+)?$':'UD','[+-](?=[0-9]\d*(\.\d+))':'SD','[a-zA-Z]+':'LE','^[^A-Za-z0-9]+$':'SY'};d=[y[i]for i in list(y.keys())if re.match(i,g)]
if len(d)>0:return d[0]
else:return'ST'


It may be a bit long, but does the job pretty much perfectly. This was a really good way to improve my regular expression skills. Thanks for the challenge. :) I will try and shorten it more if I can.

• You can move import re to outside the function to save a space. (Stupid iPad keyboard with no backticks) – CalculatorFeline Apr 1 '16 at 3:12
• @CatsAreFluffy Yeah, I did not think of that. Thank you! :) – R. Kap Apr 1 '16 at 3:38
• @CatsAreFluffy: Actually, the iOS keyboard does allow backticks! I found this out the other day when needing to write markdown using my iPhone :) meta.stackexchange.com/questions/133673/… – homersimpson Apr 1 '16 at 4:41
• @CatsAreFluffy Nice! That's good to know. – R. Kap Apr 1 '16 at 4:41
• Also len(d)>0==d>[] – CalculatorFeline Apr 1 '16 at 14:47

# Tcl 414 Bytes

proc a b {
if {[string index $b 0] eq "+" || [string index$b 0] eq "-"} {
} elseif {[string match {[A-Za-z]} $b]} { return LE } elseif {[regexp {^(?![+-]?\d+(\.\d+)?$)..+} $b]} { return ST } elseif {[regexp {[^a-zA-Z0-9.]}$b]} {
if {[string match *.* $b]} { return$c\U
return $c\I } } puts [a$argv]