Compare commits
4 Commits
master
...
testing-af
Author | SHA1 | Date | |
---|---|---|---|
0bb33a13c2 | |||
7897f2130b | |||
a45482ca5e | |||
b37a337e06 |
@ -1,3 +1,5 @@
|
|||||||
# palindrome
|
# palindrome
|
||||||
|
|
||||||
Just run it with `python palindrome.py -i 10000` for example
|
Run script with `./palindrome.py -i 123`
|
||||||
|
|
||||||
|
Test the script with `./test_palindrome.py`
|
13
palindrome.py
Normal file → Executable file
13
palindrome.py
Normal file → Executable file
@ -12,22 +12,25 @@ def parse_args():
|
|||||||
'transform it into a'
|
'transform it into a'
|
||||||
'palindrome'))
|
'palindrome'))
|
||||||
parser.add_argument('-i',
|
parser.add_argument('-i',
|
||||||
'--int',
|
'--integer',
|
||||||
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
|
# NOTE: The argument must be an integer. Because of this constrain integers
|
||||||
# with leading zeroes will be stripped automatically in python 3.
|
# with leading zeroes will be stripped automatically in python 3. Also the
|
||||||
|
# script only allows integers as an input defined by type=int
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
if args.int <= 10000 and args.int > 0:
|
if args.integer <= 10000 and args.integer > 0:
|
||||||
print('Input integer is: {}'.format(args.int))
|
print('Input integer is: {}'.format(args.integer))
|
||||||
transform = Transform()
|
transform = Transform()
|
||||||
palindrome = transform.palindrome(args.int)
|
palindrome = transform.palindrome(args.integer)
|
||||||
|
if palindrome == transform.INVALID_RETURN_VALUE:
|
||||||
|
print('Palindrome to be calculated is to big. Abort!')
|
||||||
print("Final Output is:", palindrome)
|
print("Final Output is:", palindrome)
|
||||||
return palindrome
|
return palindrome
|
||||||
else:
|
else:
|
||||||
|
49
test_palindrome.py
Executable file
49
test_palindrome.py
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import unittest
|
||||||
|
from transform.Transform import Transform
|
||||||
|
|
||||||
|
|
||||||
|
class TestPalindrome(unittest.TestCase):
|
||||||
|
"""Test the palindrome script."""
|
||||||
|
PALINDROME_MAIN_TEST_CASES = [{'input': 28, 'output': 121},
|
||||||
|
{'input': 51, 'output': 66},
|
||||||
|
{'input': 11, 'output': 11},
|
||||||
|
{'input': 607, 'output': 4444},
|
||||||
|
{'input': 196, 'output': -1}]
|
||||||
|
|
||||||
|
IS_PALIDNROME_TEST_CASES = [{'input': 11, 'output': True},
|
||||||
|
{'input': 112, 'output': False},
|
||||||
|
{'input': 2222, 'output': True},
|
||||||
|
{'input': 0, 'output': True},
|
||||||
|
{'input': 1234321, 'output': True}]
|
||||||
|
|
||||||
|
REVERSED_TAST_CASES = [{'input': 123, 'output': 321},
|
||||||
|
{'input': 4321, 'output': 1234}]
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_palindrome_main(self):
|
||||||
|
transform = Transform()
|
||||||
|
for test_case in self.PALINDROME_MAIN_TEST_CASES:
|
||||||
|
self.assertEqual(transform.palindrome(test_case['input']),
|
||||||
|
test_case['output'])
|
||||||
|
|
||||||
|
def test_is_palindrome(self):
|
||||||
|
transform = Transform()
|
||||||
|
for test_case in self.IS_PALIDNROME_TEST_CASES:
|
||||||
|
# set input_integer for class manually to run this test
|
||||||
|
transform.input_integer = test_case['input']
|
||||||
|
self.assertEqual(transform._Transform__is_palindrome(test_case['input']), # noqa
|
||||||
|
test_case['output'])
|
||||||
|
|
||||||
|
def test__reverese_int(self):
|
||||||
|
transform = Transform()
|
||||||
|
for test_case in self.REVERSED_TAST_CASES:
|
||||||
|
self.assertEqual(transform._Transform__reverese_int(test_case['input']), # noqa
|
||||||
|
test_case['output'])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -2,56 +2,57 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
class Transform(object):
|
class Transform(object):
|
||||||
"""Class to transform a given integer into a palindrome"""
|
"""Class to transform a given integer into a palindrome"""
|
||||||
|
# to avoid magic numbers.
|
||||||
|
MAXIMUM_INPUT_VALUE = 1000000000
|
||||||
|
INVALID_RETURN_VALUE = -1
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Transform, self).__init__()
|
super(Transform, self).__init__()
|
||||||
|
|
||||||
def palindrome(self, int):
|
def palindrome(self, integer):
|
||||||
'''
|
'''
|
||||||
Kind of the main function. Checks if input int is already a palindrome
|
The main function. Checks if input integer is already a palindrome
|
||||||
and trys calculation one if input integer is not already a palindrome.
|
and trys calculating one if input integer is not already a palindrome.
|
||||||
'''
|
'''
|
||||||
self.input_int = int
|
self.input_integer = integer
|
||||||
if self.__is_palindrome(self.input_int):
|
if self.__is_palindrome(self.input_integer):
|
||||||
print('Integer {} is a palindrome'.format(self.input_int))
|
print('Integer {} is a palindrome'.format(self.input_integer))
|
||||||
return self.input_int
|
return self.input_integer
|
||||||
else:
|
else:
|
||||||
print('Integer {} is not a palindrome'.format(self.input_int))
|
print('Integer {} is not a palindrome'.format(self.input_integer))
|
||||||
print('Calculating the palindrome for input {}'.format(self.input_int))
|
print('Calculating the palindrome for input {}'.format(self.input_integer)) # noqa
|
||||||
palindrome = self.__calculate_palindrome()
|
palindrome = self.__calculate_palindrome()
|
||||||
return palindrome
|
return palindrome
|
||||||
|
|
||||||
# The following methods are private. Signaled by the leading __
|
# The following methods are private. Signaled by the leading __
|
||||||
def __is_palindrome(self, int_to_check):
|
def __is_palindrome(self, int_to_check):
|
||||||
reversed = self.__reverese_int(int_to_check)
|
reversed_integer = self.__reverese_int(int_to_check)
|
||||||
if self.input_int == reversed:
|
if self.input_integer == reversed_integer:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __reverese_int(self, int_to_reverse):
|
def __reverese_int(self, int_to_reverse):
|
||||||
reversed = 0
|
reversed_integer = 0
|
||||||
int_to_check = int_to_reverse
|
int_to_check = int_to_reverse
|
||||||
while int_to_check > 0:
|
while int_to_check > 0:
|
||||||
reminder = int_to_check % 10
|
reminder = int_to_check % 10
|
||||||
reversed = (reversed * 10) + reminder
|
reversed_integer = (reversed_integer * 10) + reminder
|
||||||
int_to_check = int_to_check // 10
|
int_to_check = int_to_check // 10
|
||||||
return reversed
|
return reversed_integer
|
||||||
|
|
||||||
def __calculate_palindrome(self):
|
def __calculate_palindrome(self):
|
||||||
reversed = self.__reverese_int(self.input_int)
|
reversed_integer = self.__reverese_int(self.input_integer)
|
||||||
self.input_int = self.input_int + reversed
|
self.input_integer = self.input_integer + reversed_integer
|
||||||
print('Possible palindrome is {}'.format(self.input_int))
|
print('Possible palindrome is {}'.format(self.input_integer))
|
||||||
|
if self.__is_palindrome(self.input_integer) is True:
|
||||||
if self.__is_palindrome(self.input_int) is True:
|
print('Possible palindrome is correct!')
|
||||||
return self.input_int
|
return self.input_integer
|
||||||
|
elif not self.__is_palindrome(self.input_integer):
|
||||||
if self.__is_palindrome(self.input_int) is False:
|
print('Possible palindrome is wrong! Calcualte again!')
|
||||||
return self.__try_again()
|
while not self.__is_palindrome(self.input_integer):
|
||||||
|
if self.input_integer >= self.MAXIMUM_INPUT_VALUE:
|
||||||
def __try_again(self):
|
return self.INVALID_RETURN_VALUE
|
||||||
while self.input_int <= 1000000000:
|
else:
|
||||||
self.__calculate_palindrome()
|
self.__calculate_palindrome()
|
||||||
if self.input_int >= 1000000000:
|
return self.input_integer
|
||||||
print('Palindrome to be calculated is to big. Abort!')
|
|
||||||
return -1
|
|
||||||
|
Loading…
Reference in New Issue
Block a user