31
\$\begingroup\$

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

\$\endgroup\$
6
  • 6
    \$\begingroup\$ Go back to the time the number was a string, then reverse the string \$\endgroup\$
    – pmg
    Jun 11 '11 at 10:54
  • 2
    \$\begingroup\$ The idea of a "shortest algorithm" is somewhat specious, especially if you'll allow "any language." Think up an algorithm, and I'll give you a DSL with an appropriate "~" operator ... \$\endgroup\$
    – Pointy
    Jun 11 '11 at 10:56
  • 3
    \$\begingroup\$ Just a notice: any number ending with 0 becomes a shorter number of digits when reversed... \$\endgroup\$
    – powtac
    Jun 11 '11 at 12:44
  • 44
    \$\begingroup\$ I know an algorithm that takes no time at all, but only works on palindromic numbers ;) \$\endgroup\$
    – schnaader
    Jun 11 '11 at 14:47
  • \$\begingroup\$ Found time to do the re-write myself. I hope this remain the puzzle that eltond meant to pose. \$\endgroup\$ Jun 12 '11 at 0:37

64 Answers 64

1 2
3
0
\$\begingroup\$

Python 3, 33 bytes

I decided to make a function instead of hardcoding it.

def r(n):print(int(str(n)[::-1]))

The code is pretty self-explanatory.

Usage: r(12345)

\$\endgroup\$
0
\$\begingroup\$

Oracle SQL 11.2, 35 34 bytes

SELECT 0+REVERSE(:1||'')FROM DUAL;
\$\endgroup\$
-1
\$\begingroup\$

JavaScript (ES6) 35

a=b=>b.split("").reverse().join("")

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't assume b is an integer. \$\endgroup\$ Jul 4 '15 at 18:54
-1
\$\begingroup\$

Javascript, 34

Replace n with your number

([]+n).split("").reverse().join("")
\$\endgroup\$
2
  • 2
    \$\begingroup\$ The brief says you have to have your code accept a number, this just looks like a snippet of code... \$\endgroup\$ Sep 4 '14 at 23:16
  • \$\begingroup\$ This is a block of code. OP says function or similar. \$\endgroup\$ Jul 4 '15 at 18:55
1 2
3