4
1
Implement an integer parser in the shortest code possible, with the following restrictions:
The function or program can only take one argument: a string (or equivalent). Assume it to be signed and always valid (don't worry about decimals, etc.). The input may optionally omit the sign, in which it is assumed to be positive.
The function or program must output an integer (or equivalent).
No pattern matching, regular expressions, etc. are allowed. This includes utilities such as
tr
for *nix.No string parsing utilities are permitted, even from the standard library. This includes, but is not limited to,
parseInt()
from JavaScript,int()
in Python, andInt32.parse()
from VBScript.The only permitted predefined string manipulation functions, methods, etc. fall into one of the following categories:
- extract a substring (e.g.
substr()
,slice()
) - get code point from character, vice versa (e.g.
toCodePoint()
,fromCodePoint()
) - concatenate strings (e.g.
append()
,concat()
,strcpy()
)
- extract a substring (e.g.
No external libraries are permitted.
No string parsing utilities are permitted, even from the standard library.
Implicit conversion to string is disallowed (e.g.
1+''
in JavaScript)
Shortest code wins, so happy golfing!
EDIT: Sorry for the confusion, but implicit conversion is also disallowed.
What if the input string isn't an integer? what to output? – Adam Speight – 2014-05-30T06:50:05.490
Addressed. Question updated. – Isiah Meadows – 2014-05-30T06:52:12.317
Both positive and negative? – Adam Speight – 2014-05-30T06:53:20.793
Also addressed. Assume it's already valid and is signed. – Isiah Meadows – 2014-05-30T07:00:33.527
Is
split
an extract a substring method? – Peter Taylor – 2014-05-30T08:35:59.250Okay, I'm going to use
0 + $string
in PHP (implicit integer cast). Or is using language syntax for string parsing forbidden too? :-P – bwoebi – 2014-06-01T14:05:44.817@PeterTaylor Yes. – Isiah Meadows – 2014-06-01T19:43:57.443
@bwoebi That is forbidden. I apologize for missing the implicit string conversion side of things. I'm a little embarassed for not even catching that, considering my primary language of choice (JavaScript) uses that on a regular basis. – Isiah Meadows – 2014-06-01T19:45:44.447
"The function or program must output an integer": how can you distinguish an integer output from a string one? – Blackhole – 2014-06-01T20:01:18.307
1@Blackhole This basically excludes languages that don't have an integer type. – Isiah Meadows – 2014-06-01T21:23:31.257
Here is a short PostScript function that will convert a string to an integer:
exec
. (It even allows you to express it symbolically as the result of a computation!) – AJMansfield – 2014-06-02T12:24:28.8331To be pedantic, you haven't specified how (or if) the output integer should correspond to the input string, so a solution that always outputs 42 would seem to be technically valid, as 42 is definitely an integer. – Ilmari Karonen – 2014-06-02T19:11:51.290
1Also, you might want to include some examples of valid and invalid input strings. For example, I'd like to know which of the following strings are valid inputs:
0
,1
,-1
,+1
,+-1
,-0
,+0
,0000
,0001
,+
,-
, (empty string),- 1
,123 456
,9999999999
,2147483648
,-2147483648
. – Ilmari Karonen – 2014-06-02T19:17:18.153@AJMansfield: If that's allowed, I can beat it in GolfScript:
~
– Ilmari Karonen – 2014-06-02T20:03:43.653Do we have to treat both
123
and+123
, or does one of them suffice? (if so, I presume the former suffices) – FireFly – 2014-06-02T21:44:49.240@ajmansfield, ilmari Not allowed. – Isiah Meadows – 2014-06-03T22:28:20.567
@firefly Either one. – Isiah Meadows – 2014-06-03T22:28:40.710
The
exec
is effectively equivalent to aparseInt
method. – Isiah Meadows – 2014-06-03T22:30:29.097