15
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Can SY be more than one character? \$\endgroup\$ – FryAmTheEggman Mar 31 '16 at 15:27
  • \$\begingroup\$ I would not consider 111111111111111111111111111111111111111111 to be of integer type. \$\endgroup\$ – Matt Mar 31 '16 at 16:31
  • \$\begingroup\$ @FryAmTheEggman sy is only one character. \$\endgroup\$ – Stewie Griffin Mar 31 '16 at 17:01
  • \$\begingroup\$ So we take the input as a string? \$\endgroup\$ – lirtosiast Mar 31 '16 at 17:03
  • 6
    \$\begingroup\$ @Matt, It might not be a uint8 or int64, but it's definitely an integer. \$\endgroup\$ – Stewie Griffin Mar 31 '16 at 21:35

10 Answers 10

0
\$\begingroup\$

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

Test Suite.

\$\endgroup\$
5
\$\begingroup\$

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>

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

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

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
\$\endgroup\$
  • 4
    \$\begingroup\$ You can change ^[a-zA-Z]$ to i`^[a-z]$ to save one byte \$\endgroup\$ – daavko Mar 31 '16 at 17:01
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Surely the regex can be golfed with a regex eval(`/regex/`) constructor + template strings \$\endgroup\$ – Downgoat Mar 31 '16 at 22:47
0
\$\begingroup\$

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");}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Kevin Cruijssen Nov 16 '17 at 12:34
0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 1. should be ST, not UD. Change your \d* to \d+ \$\endgroup\$ – edc65 Mar 31 '16 at 23:23
  • \$\begingroup\$ @edc65 How? That's a decimal. That is the same as 1.0. \$\endgroup\$ – Ismael Miguel Mar 31 '16 at 23:24
  • \$\begingroup\$ 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 \$\endgroup\$ – edc65 Mar 31 '16 at 23:26
  • \$\begingroup\$ @edc65 You're right. I skipped the VERY huge list. I've fixed it. Thanks! \$\endgroup\$ – Ismael Miguel Mar 31 '16 at 23:26
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ You can move import re to outside the function to save a space. (Stupid iPad keyboard with no backticks) \$\endgroup\$ – CalculatorFeline Apr 1 '16 at 3:12
  • \$\begingroup\$ @CatsAreFluffy Yeah, I did not think of that. Thank you! :) \$\endgroup\$ – R. Kap Apr 1 '16 at 3:38
  • \$\begingroup\$ @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/… \$\endgroup\$ – homersimpson Apr 1 '16 at 4:41
  • \$\begingroup\$ @CatsAreFluffy Nice! That's good to know. \$\endgroup\$ – R. Kap Apr 1 '16 at 4:41
  • \$\begingroup\$ Also len(d)>0==d>[] \$\endgroup\$ – CalculatorFeline Apr 1 '16 at 14:47
0
\$\begingroup\$

Tcl 414 Bytes

ungolfed implementation, readable:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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