একটি একক পূর্ণসংখ্যার মূল্যবান আর্গুমেন্ট গ্রহণ করার জন্য একটি ফাংশন (বা সমমানের সাবপ্রগ্রাম) লিখুন এবং যুক্তির বেস -10 ডিজিটের ক্রমকে বিপরীত করে পাওয়া (একই ধরণের টাইপযুক্ত) মানটি ফিরিয়ে দিন।
উদাহরণস্বরূপ 76543 34567 প্রদান করুন
একটি একক পূর্ণসংখ্যার মূল্যবান আর্গুমেন্ট গ্রহণ করার জন্য একটি ফাংশন (বা সমমানের সাবপ্রগ্রাম) লিখুন এবং যুক্তির বেস -10 ডিজিটের ক্রমকে বিপরীত করে পাওয়া (একই ধরণের টাইপযুক্ত) মানটি ফিরিয়ে দিন।
উদাহরণস্বরূপ 76543 34567 প্রদান করুন
উত্তর:
‮n
replace n
with your number
Your search -
- did not match any documents.
data:text/html,&%238238;egnahcxEkcatS olleH
Python
int(str(76543)[::-1])
EDIT:
Shorter solution as suggested by @gnibbler:
int(`76543`[::-1])
or, if above is unclear:
x=76543
int(`x`[::-1])
s[::-1]
is a lot faster than ''.join(reversed(s))
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):
x = 76543 # or whatever is your number
y = 0
while x > 0:
y *= 10
y += ( x %10 )
x /= 10 # int division
This is python, but it could be done in any language, because it's just a math method.
mod
with %
, it's valid Python ;)
y=y*10+x%10
....
".|.":y
Where y is your value.
|.&.":
"reverse under do" which is pretty much a literal translation of the task.
Complete runnable program:
N.@
Where N
is your number. Rules say "accept a single integer valued argument"; In Befunge you can only enter integers from 0 to 9.
Inspired by Kiril Kirov's answer above. I got curious about the mathematical properties of reversing a number, so I decided to investigate a bit.
Turns out if you plot the difference n - rev(n)
for natural numbers n
in some base r
, you get patterns like this ((n - rev(n)) / (r - 1)
, for r=10
, wrapped at r
columns, red denotes negative number):
This sequence could be generated as such (pseudocode):
for i=1 to r:
output 0
for m=0, 1, …
for k=1 to (r-1):
for d=1 to r^m:
for i=0 to (r-1):
output (r-1) * (r+1)^m * (k - i)
If you store these values in a list/array, then n - arr[n]
would get you the reversed form of n
. Now, to "mathematically golf" this, we'd ideally want a closed-form expression that gives us the n:th value in the sequence, so that we could have a closed-form expression for solving the entire task. Unfortunately, I haven't been able to find such an expression... but it looks like it should be possible. :(
So yeah, not so much a code-golf as a mathematical curiosity, but if there is a closed-form expression of the above sequence it might actually be useful in proper PL golf submissions.
f=read.reverse.show.(+0)
f=read.reverse.show.(+0)
?
(+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.
:se ri<CR>C<C-R>"
x = 13456
x.to_s.reverse
.to_s.reverse.to_i
to comply with spec.
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:
int n = 76543;
int r = 0;
while (n > 0) {
r *= 10;
r += n % 10;
n /= 10;
}
1111111119
`-1%~
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:
{`-1%~}:r
`-1%~
rather than `-1$~
(and I've taken the liberty of editing your answer to say so).
In shell scripting :
echo "your number"|rev
Hope this was useful :)
rev<<<yournumber
, e.g. rev<<<132
(for bash/zsh, not per POSIX though)
rev
is enough, the question doesn't say it has to be a function. You could compare rev
to a built-in function, even though it's not one.
Kinda late but
⍎⌽⍞
If you insists on a function
⍎∘⌽∘⍕
IntegerReverse
This is not competing, because this function was only added in last week's 10.3 release, but for completeness I thought I'd add the only ever (I think?) built-in for this task.
You could do the following in Java. Note that this converts to String and back and is not a mathematical solution.
public class test {
public static int reverseInt(int i) {
return Integer.valueOf((new StringBuffer(String.valueOf(i))).reverse().toString());
}
public static void main(String[] args) {
int i = 1234;
System.out.println("reverse("+i+") -> " + reverseInt(i));
}
}
43 characters. num as the parameter to the function:
num.toString().split('').reverse().join('')
(first(list(parse-integer(reverse(write-to-string '4279)))))
will get you 9724.
(first(list
? parse-integer
already returns the number.
#
+#(.*)(.)/\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.
#
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.