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
|
This question came from our site for professional and enthusiast programmers.
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
|
||
|
U could try this link in browser:
data:text/html,&%238238;egnahcxEkcatS olleH
–
F. Hauri
Dec 1 '13 at 12:20
|
||
|
Funny in google transate too. @JoeFish: I can't reproduce, please post a link!
–
F. Hauri
Dec 1 '13 at 12:27
|
Python
EDIT: Shorter solution as suggested by @gnibbler:
or, if above is unclear:
| |||||||||||||||||||||
|
Universal (language agnostic/independent) If you want to use only numbers (avoid converting the number to string) and don't want to use some specific library (to be universal for any language):
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
|
Vim17 chars
| |||||
|
PHP, 9 chars
To do it short where
| ||||
|
J - 6 characters + variable
Where y is your value. | ||||
|
Ruby (14)
| |||||||||||||||||
|
Python:
| |||||
|
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:
| |||
|
Java
| ||||
|
Groovy
| |||
|
Perl, 11 charsThe Usage:
| |||||||||
|
Clojure (42 chars)
Example usage:
returns 12389 | |||
|
APL (3)
Usage:
| |||
|
Befunge (3 characters)Complete runnable program:
Where | |||||
|
Kinda late but APL, 3
If you insists on a function
| |||||||||
|
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. | |||
|