Feb-12-2021, 11:21 PM
(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