#!/usr/bin/env python # -*- coding: utf-8 -*- class Transform(object): """Class to transform a given integer into a palindrome""" # to avoid magic numbers. MAXIMUM_INPUT_VALUE = 1000000000 INVALID_RETURN_VALUE = -1 def __init__(self): super(Transform, self).__init__() def palindrome(self, integer): ''' The main function. Checks if input integer is already a palindrome and trys calculating one if input integer is not already a palindrome. ''' self.input_integer = integer if self.__is_palindrome(self.input_integer): print('Integer {} is a palindrome'.format(self.input_integer)) return self.input_integer else: print('Integer {} is not a palindrome'.format(self.input_integer)) print('Calculating the palindrome for input {}'.format(self.input_integer)) # noqa palindrome = self.__calculate_palindrome() return palindrome # The following methods are private. Signaled by the leading __ def __is_palindrome(self, int_to_check): reversed_integer = self.__reverese_int(int_to_check) if self.input_integer == reversed_integer: return True else: return False def __reverese_int(self, int_to_reverse): reversed_integer = 0 int_to_check = int_to_reverse while int_to_check > 0: reminder = int_to_check % 10 reversed_integer = (reversed_integer * 10) + reminder int_to_check = int_to_check // 10 return reversed_integer def __calculate_palindrome(self): reversed_integer = self.__reverese_int(self.input_integer) self.input_integer = self.input_integer + reversed_integer print('Possible palindrome is {}'.format(self.input_integer)) if self.__is_palindrome(self.input_integer) is True: print('Possible palindrome is correct!') return self.input_integer elif not self.__is_palindrome(self.input_integer): print('Possible palindrome is wrong! Calcualte again!') while not self.__is_palindrome(self.input_integer): if self.input_integer >= self.MAXIMUM_INPUT_VALUE: return self.INVALID_RETURN_VALUE else: self.__calculate_palindrome() return self.input_integer