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

2
\$\begingroup\$

BaCon

PRINT REVERSE$(STR$(76543))

Replace 76543 with any number.

\$\endgroup\$
2
\$\begingroup\$

Java

""+new StringBuilder(""+i).reverse();
\$\endgroup\$
2
\$\begingroup\$

Brainfuck, 11

>,[>,]<[.<]
\$\endgroup\$
1
  • \$\begingroup\$ This accepts any string, not just numbers (ASCII 48-57). \$\endgroup\$ Commented Apr 16, 2016 at 8:00
2
\$\begingroup\$

K, 3 bytes:

.|$

Evaluate (.) the reverse (|) of casting to a string ($).

Usage example:

  .|$76543
34567
\$\endgroup\$
2
\$\begingroup\$

rs, 20 bytes

#
+#(.*)(.)/\2#\1
#/

Technically, this doesn't count (rs was created earlier this year), but I didn't see any other regex-based answers, and I thought this was neat.

Live demo.

Explanation:

#

Insert a pound character at the beginning of the string. This is used as a marker.

+#(.*)(.)/\2#\1

Continuously prepend the last character of the main string to the area before the marker until there are no characters left.

#/

Remove the marker.

\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 35 32 31 (or 28) bytes

Takes input from the calculator's answer variable, which is whatever was last evaluated (like _ in the interactive python shell).

fPart(.1int(Ans10^(seq(A,A,~int(log(Ans+.5)),not(Ans
sum(Ans10^(cumSum(1 or Ans

10^( is 2NDLOG and ~ is ( - ), next to ENTER. Everything else can be found in the 2ND0 catalog.


If the program doesn't have to handle the possibility of 0 as input, it can be reduced to 28 bytes by changing ~int(log(Ans+.5)),not(Ans to ~int(log(Ans)),0.
Credit goes partially to Thomas Kwa for helping to golf this.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Doesn't work with 0; you could easily fix this by replacing log(Ans) with log(Ans+.5) and int( with iPart(. The shortest I found in a few minutes with a forwards list of digits was fPart(.1int(Ans10^(seq(A,A,~int(log(Ans)),0:sum(Ans10^(cumSum(1 or Ans; this can probably be improved. Overall, good work! \$\endgroup\$
    – lirtosiast
    Commented Jun 23, 2015 at 1:04
  • \$\begingroup\$ @ThomasKwa yep, using 0 for the end argument throws ERR:INCREMENT. You're right about not(Ans, though. \$\endgroup\$
    – user39326
    Commented Jun 23, 2015 at 4:55
2
\$\begingroup\$

ES6 Javascript, 31 bytes

x=>[...x+''].reverse().join('')
\$\endgroup\$
1
  • \$\begingroup\$ You could save 2 bytes by using .join`` instead of .join(''). Also, what do you mean with that comment: "This doesn't assume b is an integer." \$\endgroup\$
    – Stefnotch
    Commented Oct 22, 2015 at 17:20
2
\$\begingroup\$

C++:

int reverse(int number, int number1, int number2){std::vector<int> v; v.push_back(number); v.push_back(number1); v.push_back(number2); std::cout << v[2] << v[1] << v[0]; return 0;}
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf letsch! It is customary to include the byte count along with your submission, as this question is a code-golf. You might want to format your header as such: # C++, <number of bytes> bytes \$\endgroup\$ Commented Oct 22, 2015 at 17:09
  • 1
    \$\begingroup\$ Adding to what Conor said, you should also attempt to make your code as short as possible since this is a code golf competition. You could start by removing unnecessary whitespace and using single letter names. \$\endgroup\$
    – Alex A.
    Commented Oct 22, 2015 at 18:58
2
\$\begingroup\$

Pyth (3 chars)

Try it here

v_z
\$\endgroup\$
0
1
\$\begingroup\$

C#

int reversed = Convert.ToInt32(String.Join<char>(null, 76543.ToString().Reverse()));
\$\endgroup\$
2
  • \$\begingroup\$ int.Parse should be shorter. \$\endgroup\$
    – Joey
    Commented Jun 12, 2011 at 10:24
  • \$\begingroup\$ An anonymous user proposed editing this to int reversed = int.Parse(string.Join("", ("" + 76543).Reverse())); \$\endgroup\$ Commented Sep 28, 2011 at 8:03
1
\$\begingroup\$

k4 - 6 characters

"I"$|$

Examples:

  "I"$|$76543
34567
  "I"$|$98765
56789

Explanation from right to left: ("I"$ = cast to integer)(| = reverse)($ = convert to string)

\$\endgroup\$
1
  • \$\begingroup\$ It's possible to use . (eval) instead of "I"$, i.e. .|$. \$\endgroup\$
    – coltim
    Commented Oct 23, 2020 at 16:31
1
\$\begingroup\$

Perl (19 chars)

Simple:

$x=reverse (98765);

\$\endgroup\$
1
\$\begingroup\$

Bash (15)

read a;rev<<<$a

As a number of other entries do, '01234' becomes '43210' and '2340' becomes '0432'; i.e. in Python terms it does print reverse(raw_input()). If behaviour like print int(reverse(str(int(raw_input())))) is expected, it is a bit longer:

Takes care of trailing zeroes (36)

read a;sed s.^0*..\;s.0*$..<<<$a|rev
\$\endgroup\$
5
  • \$\begingroup\$ I think right count is 3: rev alone will suffice \$\endgroup\$ Commented Dec 1, 2013 at 13:24
  • \$\begingroup\$ “Write a function” – So I think the shortest solution is still 15 characters, but this way: r(){ rev<<<$1;}. (And even that is kind of cheating as the complete solutions is r(){ return `rev<<<$1`;}.) \$\endgroup\$
    – manatwork
    Commented Dec 2, 2013 at 9:40
  • 1
    \$\begingroup\$ @manatwork Shorter versions: r()(rev<<<$1) or r()(exit `rev<<<$1`). \$\endgroup\$
    – jimmy23013
    Commented Jul 1, 2014 at 21:20
  • \$\begingroup\$ Cool trick, @user23013. \$\endgroup\$
    – manatwork
    Commented Jul 2, 2014 at 10:57
  • \$\begingroup\$ str(int(raw_input())) really? [Python 2] [str(int(input()))] [from your example print int(reverse(str(int(raw_input()))))] \$\endgroup\$ Commented Apr 16, 2016 at 7:58
1
\$\begingroup\$

in Q (20 characters)

f:{"I"$reverse -3!x}

Sample Usage:

q)f 89478237
73287498

Use the k version of reverse (wrapped in parentheses) to make it shorter

{"I"$(|:) -3!x}

also just define it as a lambda to take 2 chars off for a total of 15

\$\endgroup\$
1
\$\begingroup\$

ruby (26)

The existing ruby answer wasn't a function/lambda, so here goes:

f=->i{i.to_s.reverse.to_i}
\$\endgroup\$
1
\$\begingroup\$

Haskell, 61,23

Using the string reversing technique. digitToInt is available from the Data.Char library, and of course this code would be shorter if we'd be able to assume if Data.Char already was available.

-- original version 61 chars
foldl1((+).(*)10).map(Data.Char.digitToInt)$reverse$show 1234

--bonus foldonly version, 67 chars
foldl1((+).(*)10).foldl(flip((:).Data.Char.digitToInt))[]$show 1234

--FireFly's suggestion: 23 chars. Note that read's return type is `a`
-- so you might want to tack on a +1 on ghci etc so `Int` can be derived.
read.reverse.show$1234
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hm, why not read it back after reversing? I.e. read.reverse.show$1234. \$\endgroup\$
    – FireFly
    Commented Dec 1, 2013 at 1:19
  • \$\begingroup\$ hmm for some silly reason I thought read had to do with IO monads. You're right, read would work. \$\endgroup\$ Commented Dec 1, 2013 at 1:51
  • 1
    \$\begingroup\$ Added it. fold was more fun though :) \$\endgroup\$ Commented Dec 1, 2013 at 1:55
1
\$\begingroup\$

Groovy 32

def r(n){print ((n+"").reverse())}
\$\endgroup\$
1
\$\begingroup\$

POWERSHELL, 25

-join(Read-Host)[-1..-9]

a longer version to take arbitrary input , 36

-join($a=Read-Host)[-1..-$a.length]

usage

PS C:\> -join(Read-Host)[-1..-9]
123456789
987654321
PS C:\>
\$\endgroup\$
1
\$\begingroup\$

C# - 58

I think we need more C#

int.Parse(string.Concat(Enumerable.Reverse(x.ToString())))
\$\endgroup\$
2
  • 2
    \$\begingroup\$ The spec requires a function or equivalent subprogram (a lambda would probably be acceptable, but a statement isn't); and it requires the return value to be similarly typed to the input (i.e. you're missing an int.Parse or a Convert.ToInt32). \$\endgroup\$ Commented Jul 4, 2015 at 6:39
  • 1
    \$\begingroup\$ @CoolerRanch still a statement I think \$\endgroup\$ Commented Apr 16, 2016 at 8:06
1
\$\begingroup\$

Uiua, 4 bytes

⍜°⋕⇌

Try this online!

Explanation:

⍜°⋕⇌
⍜    => under (do 1st func, then the 2nd, then undo the 1st)
 °⋕  => number to string (1st func), undo func -> string back to number
   ⇌ => reverse (2nd func)
\$\endgroup\$
0
\$\begingroup\$

Scheme, 75 characters

(string->number(list->string(reverse(string->list(number->string 76543)))))
\$\endgroup\$
2
  • \$\begingroup\$ You can probably remove all the spaces but the last one. \$\endgroup\$
    – Omar
    Commented Nov 29, 2011 at 5:50
  • \$\begingroup\$ Count in bytes, please. \$\endgroup\$ Commented Apr 16, 2016 at 8:03
0
\$\begingroup\$

k3, 4 chars

0$|$

0$|$76543 \-> gives 34567

See also the K4 solution here. The only difference (saving two chars) is that 0$ converts to int.

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

PHP, 37

<?function f($n){return strrev($n);}

C, 44

x;m(n){for(;n;x=x*10+n%10,n/=10);return x;}
\$\endgroup\$
2
  • \$\begingroup\$ Neither of these are functions, as specified. \$\endgroup\$
    – Kevin Reid
    Commented May 1, 2012 at 21:50
  • \$\begingroup\$ @KevinReid Rectified! \$\endgroup\$
    – l0n3sh4rk
    Commented May 2, 2012 at 14:47
0
\$\begingroup\$

R (35 characters)

> cat(rev(strsplit("12345","")[[1]]))
5 4 3 2 1

With help from my friends in R chat.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 12345->54321, not 12345->5 4 3 2 1. \$\endgroup\$ Commented Apr 16, 2016 at 8:04
0
\$\begingroup\$

Game Maker Language, 44

Replace 12345 with whatever number or variable you want:

n=12345while(n>0){r=r*10+(n mod 10)n div 10}

If you want to prompt for user input, use this 56 character long code instead:

n=get_string('','')while(n>0){r=r*10+(n mod 10)n div 10}

In both pieces of code, the reverse number is stored in r

\$\endgroup\$
2
  • \$\begingroup\$ The question specifically asks for "a function (or equivalent subprogram)" which takes "a single integer valued argument". Neither of your code snippets meet that spec. \$\endgroup\$ Commented Nov 30, 2013 at 17:27
  • \$\begingroup\$ @PeterTaylor They're assumed to be scripts (function equivalents). \$\endgroup\$
    – Timtech
    Commented Nov 30, 2013 at 21:48
0
\$\begingroup\$

Rebol, 29

f: func[n][do reverse mold n]

Usage example in Rebol console:

>> f 76543
== 34567

If you only wanted this to work on integer input then....

f: func [n [integer!]] [do reverse mold n]
\$\endgroup\$
0
\$\begingroup\$

Julia, 23

r(x)=int(reverse("$x"))

$ interpolates an argument (or the result of a function) into a string, denoted by the double quotes. int() automatically deletes leading zeroes.

Example:

julia> x=1234567890;r(x)
987654321

or

julia> r(1234567890)
987654321
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC, 58 54

Input N:0:While N>0:Ans+E3fpart(N,10:N/10→N:End:Ans

Usage

prgmREVERSE
?598028
          820895
            Done
\$\endgroup\$
0
0
\$\begingroup\$

Burlesque, 2 bytes

Certain built-ins treat numbers as list of digits. The reverse built-in for example treats integers as a list of digits and thus reverses integers.

blsq ) 76543<-
34567

(For characters <- switches case).

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

MATL, 3 bytes (non-competing)

VPU

This uses release 10.1.0 of the language/compiler, which is the current version at the time of writing.

Try it online!

Explanation

V   % implicitly input a number and convert to string
P   % flip
U   % convert to number and implicitly display
\$\endgroup\$

Your Answer

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