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
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.
Sign up to join this communityWrite 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
$regsubex(12,/(.)/g,$mid(\A,-\n,1))
this is AS3 code, so you may need to make slight changes
function reverseData ( inData:String ):* {
var ar1 = inData.split(''); //Takes string value, split each digit into an array
var ar2 = ar1.reverse(); //Inverses the array direction
/* //OR (if not supported) [not AS3]
var ar2 = new Array();
for( var i:int = (ar1.length - 1); i > 0; i++ ) {
ar2.push( ar1[i] );
}
*/
var result = ar2.join();
return result;
}
It should work for a string equavalent (that should be easy to typecast)
For a javascript example...
function flip( inData ) {
return ( parseInt( (inData + '').split('').reverse().join('') ) );
}
alert( flip(123) );
int reversed = Convert.ToInt32(String.Join<char>(null, 76543.ToString().Reverse()));
int reversed = int.Parse(string.Join("", ("" + 76543).Reverse()));
\$\endgroup\$
Sep 28, 2011 at 8:03
k4 - 6 characters
"I"$|$
Examples:
"I"$|$76543
34567
"I"$|$98765
56789
Explanation from right to left: ("I"$ = cast to integer)(| = reverse)($ = convert to string)
.
(eval) instead of "I"$
, i.e. .|$
.
\$\endgroup\$
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:
read a;sed s.^0*..\;s.0*$..<<<$a|rev
3
: rev
alone will suffice
\$\endgroup\$
Dec 1, 2013 at 13:24
r(){ rev<<<$1;}
. (And even that is kind of cheating as the complete solutions is r(){ return `rev<<<$1`;}
.)
\$\endgroup\$
Dec 2, 2013 at 9:40
r()(rev<<<$1)
or r()(exit `rev<<<$1`)
.
\$\endgroup\$
Jul 1, 2014 at 21:20
str(int(raw_input()))
really? [Python 2] [str(int(input()))
] [from your example print int(reverse(str(int(raw_input()))))
]
\$\endgroup\$
Apr 16, 2016 at 7:58
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
The existing ruby answer wasn't a function/lambda, so here goes:
f=->i{i.to_s.reverse.to_i}
BaCon
PRINT REVERSE$(STR$(76543))
Replace 76543 with any number.
>,[>,]<[.<]
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.
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\$
Jun 23, 2015 at 1:04
not(Ans
, though.
\$\endgroup\$
x=>[...x+''].reverse().join('')
b
is an integer."
\$\endgroup\$
Oct 22, 2015 at 17:20
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;}
# C++, <number of bytes> bytes
\$\endgroup\$
Oct 22, 2015 at 17:09
Scheme, 75 characters
(string->number(list->string(reverse(string->list(number->string 76543)))))
0$|$
0$|$76543 \-> gives 34567
See also the K4 solution here. The only difference (saving two chars) is that 0$
converts to int.
<?function f($n){return strrev($n);}
x;m(n){for(;n;x=x*10+n%10,n/=10);return x;}
R (35 characters)
> cat(rev(strsplit("12345","")[[1]]))
5 4 3 2 1
With help from my friends in R chat.
12345
->54321
, not 12345
->5 4 3 2 1
.
\$\endgroup\$
Apr 16, 2016 at 8:04
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
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
read
it back after reversing? I.e. read.reverse.show$1234
.
\$\endgroup\$
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]
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:\>
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
I think we need more C#
int.Parse(string.Concat(Enumerable.Reverse(x.ToString())))
int.Parse
or a Convert.ToInt32
).
\$\endgroup\$
Jul 4, 2015 at 6:39
Input N:0:While N>0:Ans+E3fpart(N,10:N/10→N:End:Ans
Usage
prgmREVERSE
?598028
820895
Done
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).
VPU
This uses release 10.1.0 of the language/compiler, which is the current version at the time of writing.
V % implicitly input a number and convert to string
P % flip
U % convert to number and implicitly display