Final commit
This commit is contained in:
parent
350087354a
commit
b9fc858709
@ -13,19 +13,25 @@ def parse_args():
|
|||||||
'palindrome'))
|
'palindrome'))
|
||||||
parser.add_argument('-i',
|
parser.add_argument('-i',
|
||||||
'--int',
|
'--int',
|
||||||
nargs=1,
|
|
||||||
required=True,
|
required=True,
|
||||||
help='The integer to be transformed.',
|
help='The integer to be transformed.',
|
||||||
type=int)
|
type=int)
|
||||||
|
# NOTE: The argument must be an integer. Because of this constrain integers
|
||||||
|
# with leading zeroes will be stripped automatically in python 3.
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
print(args.int)
|
if args.int <= 10000 and args.int > 0:
|
||||||
print(Transform.palindrome(args.int))
|
print('Input integer is: {}'.format(args.int))
|
||||||
return args
|
transform = Transform()
|
||||||
|
palindrome = transform.palindrome(args.int)
|
||||||
|
print("Final Output is:", palindrome)
|
||||||
|
return palindrome
|
||||||
|
else:
|
||||||
|
print("Input integer is not betweent 1 and 10000")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -6,6 +6,52 @@ class Transform(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Transform, self).__init__()
|
super(Transform, self).__init__()
|
||||||
|
|
||||||
@staticmethod
|
def palindrome(self, int):
|
||||||
def palindrome(int):
|
'''
|
||||||
return int
|
Kind of the main function. Checks if input int is already a palindrome
|
||||||
|
and trys calculation one if input integer is not already a palindrome.
|
||||||
|
'''
|
||||||
|
self.input_int = int
|
||||||
|
if self.__is_palindrome(self.input_int):
|
||||||
|
print('Integer {} is a palindrome'.format(self.input_int))
|
||||||
|
return self.input_int
|
||||||
|
else:
|
||||||
|
print('Integer {} is not a palindrome'.format(self.input_int))
|
||||||
|
print('Calculating the palindrome for input {}'.format(self.input_int))
|
||||||
|
palindrome = self.__calculate_palindrome()
|
||||||
|
return palindrome
|
||||||
|
|
||||||
|
# The following methods are private. Signaled by the leading __
|
||||||
|
def __is_palindrome(self, int_to_check):
|
||||||
|
reversed = self.__reverese_int(int_to_check)
|
||||||
|
if self.input_int == reversed:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __reverese_int(self, int_to_reverse):
|
||||||
|
reversed = 0
|
||||||
|
int_to_check = int_to_reverse
|
||||||
|
while int_to_check > 0:
|
||||||
|
reminder = int_to_check % 10
|
||||||
|
reversed = (reversed * 10) + reminder
|
||||||
|
int_to_check = int_to_check // 10
|
||||||
|
return reversed
|
||||||
|
|
||||||
|
def __calculate_palindrome(self):
|
||||||
|
reversed = self.__reverese_int(self.input_int)
|
||||||
|
self.input_int = self.input_int + reversed
|
||||||
|
print('Possible palindrome is {}'.format(self.input_int))
|
||||||
|
|
||||||
|
if self.__is_palindrome(self.input_int) is True:
|
||||||
|
return self.input_int
|
||||||
|
|
||||||
|
if self.__is_palindrome(self.input_int) is False:
|
||||||
|
return self.__try_again()
|
||||||
|
|
||||||
|
def __try_again(self):
|
||||||
|
while self.input_int <= 1000000000:
|
||||||
|
self.__calculate_palindrome()
|
||||||
|
if self.input_int >= 1000000000:
|
||||||
|
print('Palindrome to be calculated is to big. Abort!')
|
||||||
|
return -1
|
||||||
|
Loading…
Reference in New Issue
Block a user