Python Forum
Reverse Function in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reverse Function in Python
#1
Hi everyone i am beginner in python i am facing with this exercice can anyone explained how to write this in Python :

Exercice :

-----------------------------

Write a function reverse that receives an integer n as a parameter and returns that integer as result backwards. For example, if n = 123456, the function returns 654321.

For that, it will be necessary to use the division and the modulo. Reminder: 153% 10 = 3 and 153/10 = 15.

Remember to handle the case n < 0.

-----------------------------------
I hope got answer from u guys.

Thank u in advanced.
Reply
#2
Would it be legal to turn it into a string, reverse it and then turn it back into an integer?

def reverse_an_integer (integer: int) -> int :
	return int (str (integer) [::-1])

print (reverse_an_integer (1234007))
buran write Feb-13-2021, 07:19 AM:
Please, don't give full solution without effort on OP part
See https://python-forum.io/misc.php?action=help&hid=52
Reply
#3
Sounds like you have to do this the correct way instead of using string method.

You can do division and modulo at the same time. Lookup divmod.

This is a really easy problem to solve once you understand how it is done. Using division/module or divmod can you figure out how to get all the individual digits that make up a number? This is a mental exercise, not a programming one.

Once you can get the digits, you need to put them back together in reverse order. If you have a list of numbers [1, 2, 3, 4, 0, 0, 7] how would you create the number 7004321? This too is mental exercise and the coding is pretty simple once you've solved the problem.
Reply
#4
(Feb-12-2021, 10:20 PM)BashBedlam Wrote: Would it be legal to turn it into a string, reverse it and then turn it back into an integer?

def reverse_an_integer (integer: int) -> int :
	return int (str (integer) [::-1])

print (reverse_an_integer (1234007))

Possibly, but you have the solution in the text:
Quote:it will be necessary to use the division and the modulo. Reminder: 153% 10 = 3 and 153/10 = 15.
Just repeat the same and use the pattern
n = 153
x = 0
x = 10 * x + 153 % 10 = 10 * 0 + 3 = 3
n = 153 // 10         = 15             # use integer division !

x = 10 * 3 + 15 % 10  = 30 + 5 = 35
n = 15 // 10          = 1

x = 10 * 35 + 1 % 10  = 350 + 1 = 351
n = 1 // 10 = 0                        # nothing left, stop

return 351
Reply
#5
Actually i told u honestly this exercice is from C programming exercice in my classe so i tried to search interenet but i block with this exercice so if u huy have exemple similar to write this reverse function in C prog.

thank u guy in advanced.
Reply
#6
Reverse Function in Python
Reply
#7
Well, it is just the pattern for how to proceed. It can be coded in any programming language. Try to turn it into code yourself. We like to help but not to give you the solution to your exercises. If you want it in C, then you are in the wrong forum...

Have you really searched the internet? There are loads of code samples of that kind in most languages. But if you want to understand something about how to think as a programmer or a computer scientist, copying is not the road to take.
buran likes this post
Reply
#8
actually i post this group maybe for get some help and if i can get concept from u guy in python code and than i will apply in C but You are right, i will try to search and turn it into code by myself . anyway thank for your advice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,359 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
  reverse math in python, who will find the correct answer? Kakha 11 4,479 Jan-11-2021, 11:59 AM
Last Post: Gribouillis
  Why this reverse lookup function not working Emekadavid 4 2,351 May-31-2020, 05:54 AM
Last Post: Emekadavid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020