Wing 101Python3 Reversing and adding numbers You will write

Wing 101-Python3

Reversing and adding numbers. You will write a Python program (called q2.py) to implement the following procedure to produce palindromic numbers. Take any positive integer x. If x is a palindrome, stop. Otherwise, continue to step (b). Reverse the digits of x. Add the reverse to x. If the sum is a palindrome, then stop. Otherwise, let x represent the sum, and repeat steps (b) through (d). Steps (b) through (d) represent a single iteration. Below, are the number of iterations required for a few numbers to become palindromic using the reverse and add procedure. 8 is a palindrome. It is palindromic after 0 iterations. 56 becomes palindromic after 1 iteration: 56 + 65 = 121. 57 becomes palindromic after 2 iterations: 57 + 75 = 132, 132 + 231 = 363. 59 becomes palindromic after 3 iterations: 59 + 95 = 154, 154 + 451 = 605, 605 + 506 = 1111. 89 takes an unusually large 24 iterations (the most of any number under 10, 000 that is known to resolve into a palindrome) to reach the palindrome 8813200023188. For 196, it is not known if the above procedure leads to a palindromic number. Your program will consist of two user-defined functions: reverse_add(low, high) and main(). The reverse_add() function implements the reverse and add numbers procedure described above for all integers in the range low to high. For example, if low is 10 and high is 50, then the function would run the reverse and add procedure on the numbers 10, 11, ..., 49, and 50. Or, the user could be interested in a single number such as 89. In this case, low and high are both 89. For each number in the range of interest, your function will either output the number of steps required for it be palindromic or report that it does not lead to a palindrome within 100 steps. If the low and high range includes a single number (i.e., 190 to 190), then your program will show each step of the reverse and add procedure. See examples for more details. The main() function drives the program. q2.py has been provided, where the code for the main() function is provided for you. You will provide the code for the reverse_add() function.

Solution

def reverse(n):
   tmp = n
   m = 0
   while tmp>0:
       m = m*10 + (tmp%10)
       tmp = tmp/10
   return m
def add(n):
   m = reverse(n)
   #print(m)
   count = 0
   while n!=m:
       n = n+m
       m = reverse(n)
       count = count+1
   return count
def add_steps(n):
   m = reverse(n)
   #count = 0
   while n!=m:
       tmp = n+m
       print(str(n)+\'+\'+str(m)+\' = \'+str(tmp))
       n=tmp
       m = reverse(n)
       #count = count+1
def reverse_add(low,high):
   if(low==high):
       add_steps(low)
       return
   for i in range(low,high+1):
       print(add(i))
       #add_steps(i)
reverse_add(10,20)

Wing 101-Python3 Reversing and adding numbers. You will write a Python program (called q2.py) to implement the following procedure to produce palindromic number

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site