Python Forum
Reverse Function in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reverse Function in Python
#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


Messages In This Thread
Reverse Function in Python - by Voraman - Feb-12-2021, 09:45 PM
RE: Reverse Function in Python - by BashBedlam - Feb-12-2021, 10:20 PM
RE: Reverse Function in Python - by Serafim - Feb-12-2021, 11:21 PM
RE: Reverse Function in Python - by deanhystad - Feb-12-2021, 11:20 PM
RE: Reverse Function in Python - by Voraman - Feb-13-2021, 06:38 PM
RE: Reverse Function in Python - by Voraman - Feb-13-2021, 06:41 PM
RE: Reverse Function in Python - by Serafim - Feb-13-2021, 06:56 PM
RE: Reverse Function in Python - by Voraman - Feb-13-2021, 07:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 4,560 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
  reverse math in python, who will find the correct answer? Kakha 11 6,007 Jan-11-2021, 11:59 AM
Last Post: Gribouillis
  Why this reverse lookup function not working Emekadavid 4 3,306 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