reversing a number | Sololearn: Learn to code for FREE!


reversing a number

it's a code of reverse a number in codechef website. i tried by writing normal reversing method which is print(y[::-1]). but it shows me it was wrong. can anyone explain me the below code? n=int(input()) for i in range(n): m=int(input()) a=abs(m) rev=0 while(a>0): b=a%10 rev=rev*10+b a=a//10 if(m>0): print(rev) else: print(-rev)

5/27/2021 12:50:39 PM

Mr. 12

10 Answers

New Answer


Mr. 12 y[::-1] is slicing. It is works for iterables not for numbers. Now take 2004 as a input: In side the while loop.... 1)b=a%10 % gives a last digit of a value a b=2004%10=4 Now , b=4 and rev =0 2)rev=rev*10+b This means rev=0*10+4=0+4=4 rev=4 So got a first digit for an reverse of the number . Next.... 3) a=a//10 // is a floor division So, a= 2004//10=200 a=200 So, we reduced an one digit in a. On the other rounds in while loops are.... Round 2: b=200%10=0 b=0 rev= 4*10+20=40+0=40 rev=40 a=200//10=20 a=20 Round 3: b=20%10=0 b=0 rev= 40*10+0=400 rev=400 a=20//10 a=2 Round 4: b=2%10=2 b=2 rev=400*10+2=4000+2=4002 rev=4002 a=2//10=0 a=0 Now, a==0 so loop was broke. In if statement it check whether a given number is +ve or -ve. And print the suitable rev for this.


here is a sample that is working for positive and negative input numbers: inp = int(input()) neg = True if inp < 0 else False inp = abs(inp) res = 0 while inp > 0: inp,rem = divmod(inp,10) res = res * 10 + rem print(res if not neg else -res)


input first the count of numbers you want to reverse... loop that count for: 1) input a number to be reversed, get its absolute value, initialise the 'rev' result variable to zero. 2) start process to reverse the number by entering the 'while' loop until a is strictly greater than zero, where: a) get last (right most) digit of it (using the remainder of while division by 10) b) multiply rev by 10 (to left shift digits by one place) and adding the previous digit got in a) c) update a by whole dividing it by 10 (remove last -- rigtht most -- digit) 3) finally output the result... with the sign of initial inputed number (before getting absolute value)


˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ you're right that slicing "works for iteravles not for numbers" however, you could use it on the string representation of a number ;)


Lothar what's the deep difference with the code provided by OP, except it implement only one reversing number and code being more consise/short (I guess that if OP hardly understand the first one, yours may not be more understandable ;P)


thank you ˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜


Why don't you get input as string and use the method you mentioned? Just check if the number is negative or not (check if there is '-' sign) and handle both cases separately


Michal Doruch with the string method, 420 reversed will become 024, while numerical method give 24 ^^ also, OP requested for explain of provided code ;P


thank you visph


Mr. 12, there are two type of errors in you code: First: you shouldn't use two variables (n and m) because the loop will be applied only for the second input m. Second: you shouldn't use a "for loop" with the range(n) because the loop will run for n times (if you define n=345 the loop will run for 345 times). This is a corrected version of your code: Remember that in Python you can convert a variable from int to str and from str to int paying attention on the fact that the string could be readable as an integer (if you reverse a negative number as a string (for example "-34") the "-" will be put at the end and the string can't be readable as an integer ("34-" can't be readable as an integer)).