33
\$\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
    Commented Jun 11, 2011 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
    Commented Jun 11, 2011 at 10:56
  • 3
    \$\begingroup\$ Just a notice: any number ending with 0 becomes a shorter number of digits when reversed... \$\endgroup\$
    – powtac
    Commented Jun 11, 2011 at 12:44
  • 46
    \$\begingroup\$ I know an algorithm that takes no time at all, but only works on palindromic numbers ;) \$\endgroup\$
    – schnaader
    Commented Jun 11, 2011 at 14:47
  • \$\begingroup\$ Found time to do the re-write myself. I hope this remain the puzzle that eltond meant to pose. \$\endgroup\$ Commented Jun 12, 2011 at 0:37

67 Answers 67

1 2
3
0
\$\begingroup\$

Python 2, 26 bytes

print int(`input()`[::-1])
\$\endgroup\$
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\$
0
\$\begingroup\$

J-uby, 6 bytes

S|:~|Z

S converts to string, :~ reverses, Z converts to integer.

Attempt This Online!

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

AWK, 25 bytes

{for(i=NF;i;i--)printf$i}
awk -F "" '{for(i=NF;i;i--)printf$i}' <<< 123
\$\endgroup\$
0
\$\begingroup\$

Python

def ans(n):
    L = str(n)
    return(int(L[::-1]))
\$\endgroup\$
-1
\$\begingroup\$

JavaScript (ES6) 35

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This doesn't assume b is an integer. \$\endgroup\$ Commented Jul 4, 2015 at 18:54
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.