Write a function (or equivalent subprogram) to accept a single integer valued argument and return a (similarly typed) value found by reversing the order of the base-10 digits of the argument.
For example given 76543 return 34567
|
HTML
|
This is just plain genius. I'd go for one char. Or 2, as it encodes to two bytes in UTF-16 :P – tomsmeding Dec 3 '12 at 13:33 |
|||
|
Hahaha I did a Google search on that tag and was rewarded with Your search - - did not match any documents. – JoeFish Dec 4 '12 at 14:32 |
|
Python
EDIT: Shorter solution as suggested by @gnibbler:
or, if above is unclear:
|
|||||||||||||||||
|
|
If you want to use only numbers (not convert it to string), and don't want to use some specific library (to be universal for any language), I'd do it like this
This is python, but it could be done in any language, because it's just a math method. |
|||||||||||||||
|
Perl 6
or:
for dynamically typed code. Numbers got string methods due to language design. |
||||
|
|
Haskell,
|
|
|||
(+0): Legit man! Though technically you don't need the .(+0) at all, as f would be more polymorphic than what the problem requires (it is allowed to return a 'similarly typed' output). I would shave off those 5 characters. – Thomas Eding Aug 10 '11 at 22:14 |
Ruby (14)
|
|||||||||||||||
|
|
Python:
|
|||
|
|
Vim17 chars
|
|||
|
Golfscript, 5 chars
This takes an argument on the stack and leaves the result on the stack. I'm exploiting the "subprogram" option in the spec: if you insist on a function, that's four chars more leaving it on the stack:
|
||||
|
|
In shell scripting :
Hope this was useful :) |
|||
|
|
|
It is possible to convert a number a string, then reverse the string and then convert that string back to number. This kind of feature is probably available in all language. If you are looking for a more mathematical method then this might help:
|
|||||||||
|
|
Scala - 33 Chars
|
|||
|
|
|
You could do the following in Java. Note that this converts to String and back and is not a mathematical solution.
|
|||||||||
|
|
You can use reversing a String in Java
|
|||||||||
|
Python 3+Function form: 28 characters
(Sub)program form: 25 characters
I consider some of the other Python examples to be cheating, or at least cheap, due to using hardcoded input and/or not fully satisfying the requirements. |
|||||
|
LuaNumbers and strings are interchangeable, so this is trivial
|
|||
|
|
Actionscript43 characters. num as the parameter to the function:
|
|||
|
|
Groovy
|
|||
|
|
Perl, 11 charsThe Usage:
|
|||
|
|
Clojure (42 chars)
Example usage:
returns 12389 |
|||
|
|
Befunge (3 characters)Complete runnable program:
Where |
|||
|
|
|
this is AS3 code, so you may need to make slight changes
It should work for a string equavalent (that should be easy to typecast) For a javascript example...
|
|||
|
|
|
Depends on what you mean by short (javascript):
|
|||||||
|
C#
|
|||||
|
|
This one ACTUALLY takes an input, unlike some of the rest:
Python btw. |
|||
|
|
|
k4 - 6 characters
Examples:
Explanation from right to left: ("I"$ = cast to integer)(| = reverse)($ = convert to string) |
||||
|
|