Reversing and adding numbers You will write a Python program
Reversing and adding numbers. You will write a Python program to implement the following procedure to produce palindromic numbers.
a) Take any positive integer x. If x is a palindrome, stop. Otherwise, continue to step (b).
b) Reverse the digits of x.
c) Add the reverse to x.
d) 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.
This is the code you should work on: Please try to explain using comments:
def reverse_add(low, high):
 \'\'\' Executes the reverse and add algorithm for 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, 50. Or, the user could be interested in a single number such as 89. In
 this case, low and high are both 89.
 \'\'\'
   
 # Write the rest of your code here.
 print(\'low:\', low)
 print(\'high:\', high)
   
   
 def main():
 \'\'\' The program driver. \'\'\'
# set cmd to anything except quit()
 cmd = \'\'
   
 # process the user commands
 cmd = input(\'> \')
 while cmd != \'quit\':
 i = 0
 while i < len(cmd) and cmd[i] != \' \':
 i += 1
 if \' \' in cmd:
 low = int(cmd[:i+1])
 high = int(cmd[i+1:])   
 else:
 low = int(cmd)
 high = low
 reverse_add(low, high)
 cmd = input(\'> \')
main()
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
def reverse(num): #function to reverse a int number
 rev = 0   
 while num > 0:
 rev = (10*rev) + num%10
 num //= 10
 return rev
def reverse_add(low, high):
 \'\'\' Executes the reverse and add algorithm for 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, 50. Or, the user could be interested in a single number such as 89. In
 this case, low and high are both 89.
 \'\'\'
   
 # Write the rest of your code here.
 print(\'low:\', low)
 print(\'high:\', high)
   
 if(low != high): #if low and high are not same numbers, means given is a range
 for num in range(low,high+1): #iterate through the given range
 steps = 0; #itialize steps
 n = num
 for i in range(0,100): #iterate the reverse and add method 100 times if reversly added digit is not palindrome
 rev = reverse(n) #get the reverse of the number
 if(n==rev): # if the number n and reverse rev are equal, means they are palindrom
 stp = \' step\'   
 if steps > 1 :
 stp = \' steps\' #add steps if step is > 1
   
 print num,\": PAL (\",steps,stp,\")\" #print output
 break
 n = n + rev # add reverse to the number n if n is not palindrome
 steps = steps+1 #increment steps
 if(n!=rev): #if n is not a palindrom
 print num,\": NOT-PAL\"
   
 elif(low == high): #if high and low are both same number, means user entered a single number, means not a range
steps = 0;
 n = low
 for i in range(0,100): #iterate the reverse and add method 100 times if reversly added digit is not palindrome
 rev = reverse(n) #get the reverse
 print steps,\". \",n,\" + \",rev,\" = \", (n+rev) #print each step
 if(n==rev):
 stp = \' step\'
 if steps > 1 :
 stp = \' steps\'
   
 print low,\": PAL (\",steps,stp,\")\" #print output
 break
 n = n + rev
 steps = steps+1
 if(n!=rev):
 print num,\": NOT-PAL\"
   
   
 def main():
 \'\'\' The program driver. \'\'\'
 # set cmd to anything except quit()
 cmd = \'\'
   
 # process the user commands
 cmd = raw_input(\'> \')
 while cmd != \'quit\':
 i = 0
 while i < len(cmd) and cmd[i] != \' \':
 i += 1
 if \' \' in cmd:
 low = int(cmd[:i+1])
 high = int(cmd[i+1:])   
 else:
 low = int(cmd)
 high = low
 reverse_add(low, high)
 cmd = raw_input(\'> \')
 main()
-----------------------------------------------------
OUTPUT:
> 10 15
 (\'low:\', 10)   
 (\'high:\', 15)
 10 : PAL ( 1 step )   
 11 : PAL ( 0 step )   
 12 : PAL ( 1 step )   
 13 : PAL ( 1 step )   
 14 : PAL ( 1 step )   
 15 : PAL ( 1 step )   
 > 84 91
 (\'low:\', 84)   
 (\'high:\', 91)
 84 : PAL ( 2 steps )
 85 : PAL ( 2 steps )
 86 : PAL ( 3 steps )
 87 : PAL ( 4 steps )
 88 : PAL ( 0 step )   
 89 : PAL ( 24 steps )   
 90 : PAL ( 1 step )
 91 : PAL ( 2 steps )
 > 190 190
 (\'low:\', 190)
 (\'high:\', 190)   
 0 . 190 + 91 = 281
 1 . 281 + 182 = 463   
 2 . 463 + 364 = 827   
 3 . 827 + 728 = 1555
 4 . 1555 + 5551 = 7106
 5 . 7106 + 6017 = 13123   
 6 . 13123 + 32131 = 45254   
 7 . 45254 + 45254 = 90508   
 190 : PAL ( 7 steps )   
 > quit   



