Is it an integer, a string, or a decimal?
Pyth, 33 bytes (39 without packed string)
@_c."at%¸Ã`9hàãáÊ"7.x/`MsB+vz0z0
Some bytes are stripped due to Markdown. Official code and test suite.
Without packed string:
@_c"integerdecimalstring"7.x/`MsB+vz0z0
It passes all of above test cases. Basically, to check if a string is a integer or decimal, it checks whether the string can be evaluated as a python literal (v
), and if so, if you can add 0 to it and covert it back to its string representation, and get the input string. If so, it's an integer or a decimal. If you can also cast it to an int and still get the original string back, it's an integer.
Javascript, 112 121 87 bytes
Thanks to @edc65 for saving 34 bytes by converting the original code (in the explanation) to ES6. I didn't change the explanation because it shows the logic better.
b=a=>/^[1-9]\d*$/.test(a)?"integer":/^([1-9]\d+|\d)\.\d+$/.test(a)?"decimal":"string"
This basically converts the rules for an integer and decimal in the question into regex checks, and tests them against the given input. If the input doesn't match, then it must be a string. It passes all of the tests given in the question.
Ungolfed + explanation
function b(a) {
if(/^[1-9]\d*$/.test(a)) // regex check for the rules of an 'integer':
return"integer"; // ^[1-9] - check to make sure the first digit
// \d* - check to make sure that it is followed by zero or more digits
// $ - ensures that the previous check continues to the end of the word
if(/^([1-9]\d+|\d)\.\d+$/.test(a)) // regex check for the rules of a 'decimal', starting from the middle
return"decimal"; // \. checks for a '.' in the word
// the ([1-9]\d+|\d) and \d+ check to make sure the '.' is surrounded by
// one or more numerical characters on each side.
// the ^,$ ensure that the match is for the whole word
return"string"; // none of the others match, so it must be a string.
}