Determine the type of an input

15

1

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

Stewie Griffin

Posted 2016-03-31T14:31:27.350

Reputation: 43 471

Can SY be more than one character? – FryAmTheEggman – 2016-03-31T15:27:42.757

I would not consider 111111111111111111111111111111111111111111 to be of integer type. – Matt – 2016-03-31T16:31:11.110

@FryAmTheEggman sy is only one character. – Stewie Griffin – 2016-03-31T17:01:45.293

So we take the input as a string? – lirtosiast – 2016-03-31T17:03:21.140

6

@Matt, It might not be a uint8 or int64, but it's definitely an integer.

– Stewie Griffin – 2016-03-31T21:35:15.733

Here +45 is not signed it is PI positive integer (as 45) ... Your classification can not be so easy to write in Axiom. "signed integer" here and in C language too are called "int" or "INT" – RosLuP – 2017-11-14T16:56:30.837

@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 – 2017-11-28T20:43:56.927

+38 is not signed it is unsigned. – RosLuP – 2017-11-29T11:24:45.467

@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 – 2017-11-29T13:22:08.967

Answers

0

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.

Maltysen

Posted 2016-03-31T14:31:27.350

Reputation: 25 023

5

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>

edc65

Posted 2016-03-31T14:31:27.350

Reputation: 31 086

1I was pretty sure that someone would be able to optimise for shared cases but I like your use of parseInt to detect letters. – Neil – 2016-04-01T00:13:34.000

The first ( ) in your regex is not necessary – Awashi – 2016-04-01T07:25:37.523

@Awashi it is necessary as I need a capture group for the sign to differentiate S or U. – edc65 – 2016-04-01T07:57:19.200

@WashingtonGuedes no, it could be (\+|-) but no bytes saved – edc65 – 2016-04-02T08:31:11.690

3

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

Leaky Nun

Posted 2016-03-31T14:31:27.350

Reputation: 45 011

3Please include a fully-golfed version of the code. If the whitespace cannot be removed, it must be counted in the byte count. – Mego – 2016-03-31T20:33:22.493

2Looks like it can not be removed, the program does not work properly without spaces between the symbols. – Matthew Smith – 2016-03-31T20:41:36.270

It doesn't seem to classify "+-1" (from the examples) as a ST. – Xantix – 2016-04-01T01:05:01.150

2

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

Leaky Nun

Posted 2016-03-31T14:31:27.350

Reputation: 45 011

4You can change ^[a-zA-Z]$ to i\^[a-z]$` to save one byte – daavko – 2016-03-31T17:01:10.613

1

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

Leaky Nun

Posted 2016-03-31T14:31:27.350

Reputation: 45 011

1

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)

Neil

Posted 2016-03-31T14:31:27.350

Reputation: 95 035

Surely the regex can be golfed with a regex eval(\/regex/`)` constructor + template strings – Downgoat – 2016-03-31T22:47:30.093

0

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");}

SuperJedi224

Posted 2016-03-31T14:31:27.350

Reputation: 11 342

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 – 2017-11-16T12:34:41.757

0

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.

Ismael Miguel

Posted 2016-03-31T14:31:27.350

Reputation: 6 797

11. should be ST, not UD. Change your \d* to \d+ – edc65 – 2016-03-31T23:23:19.173

@edc65 How? That's a decimal. That is the same as 1.0. – Ismael Miguel – 2016-03-31T23:24:15.360

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 – 2016-03-31T23:26:02.327

@edc65 You're right. I skipped the VERY huge list. I've fixed it. Thanks! – Ismael Miguel – 2016-03-31T23:26:57.343

0

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.

R. Kap

Posted 2016-03-31T14:31:27.350

Reputation: 4 730

You can move import re to outside the function to save a space. (Stupid iPad keyboard with no backticks) – CalculatorFeline – 2016-04-01T03:12:14.630

@CatsAreFluffy Yeah, I did not think of that. Thank you! :) – R. Kap – 2016-04-01T03:38:07.297

@CatsAreFluffy: Actually, the iOS keyboard does allow backticks! I found this out the other day when needing to write markdown using my iPhone :) http://meta.stackexchange.com/questions/133673/is-there-a-way-to-enter-back-tick-backtick-on-so-when-using-ipad-or-iphone

– homersimpson – 2016-04-01T04:41:10.940

@CatsAreFluffy Nice! That's good to know. – R. Kap – 2016-04-01T04:41:57.027

Also len(d)>0==d>[] – CalculatorFeline – 2016-04-01T14:47:08.123

0

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]

Legit Stack

Posted 2016-03-31T14:31:27.350

Reputation: 101