From fd21191df207ef4ef63e327509afe36755e4da2a Mon Sep 17 00:00:00 2001 From: Alex Botello Date: Mon, 16 Jan 2017 23:11:15 -0600 Subject: [PATCH 1/2] [Python] Challenge 13 --- challenge_13/python/alexbotello/README.md | 27 +++++++++++++++++++ challenge_13/python/alexbotello/src/palin.py | 23 ++++++++++++++++ challenge_13/python/alexbotello/src/tests.py | 28 ++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 challenge_13/python/alexbotello/README.md create mode 100644 challenge_13/python/alexbotello/src/palin.py create mode 100644 challenge_13/python/alexbotello/src/tests.py diff --git a/challenge_13/python/alexbotello/README.md b/challenge_13/python/alexbotello/README.md new file mode 100644 index 000000000..bdf6fd288 --- /dev/null +++ b/challenge_13/python/alexbotello/README.md @@ -0,0 +1,27 @@ +# Integer Palindrome +###### Language Version (Python 3.6.0) + + +We have our function take the integer input and convert it into a list of +integers. We find the number of digits, the midpoint, and then separate both +halves of the list. + +If the number of digits is even, reverse the second half and compare it to the +first. If number of digits are odd, follow the same steps, but be sure to add +1 +to the index of the first half. + +Run ```palin.py`` to test your implementation +``` +456070654 + +True +``` + +Run ```tests.py`` to check against unit test +``` +....... +---------------------------------------------------------------------- +Ran 7 tests in 0.001s + +OK +``` diff --git a/challenge_13/python/alexbotello/src/palin.py b/challenge_13/python/alexbotello/src/palin.py new file mode 100644 index 000000000..914acc19d --- /dev/null +++ b/challenge_13/python/alexbotello/src/palin.py @@ -0,0 +1,23 @@ +# Integer Palindrome + +def is_palindrome(num): + """ + Returns True if integer is a Palindrome + """ + num = [int(x) for x in str(num)] + digits = len(num) + mid = digits // 2 + back_half = num[mid:] + + if digits % 2 == 0: + return True if num[:mid] == back_half[::-1] else False + + else: + return True if num[:mid+1] == back_half[::-1] else False + + +if __name__ == "__main__": + + num = int(input()) + print() + print(is_palindrome(num)) diff --git a/challenge_13/python/alexbotello/src/tests.py b/challenge_13/python/alexbotello/src/tests.py new file mode 100644 index 000000000..82d40ca1a --- /dev/null +++ b/challenge_13/python/alexbotello/src/tests.py @@ -0,0 +1,28 @@ +import unittest +from palin import is_palindrome + +class Tests(unittest.TestCase): + def test_1(self): + self.assertEqual(is_palindrome(1112111), True) + + def test_2(self): + self.assertEqual(is_palindrome(1), True) + + def test_3(self): + self.assertEqual(is_palindrome(59112), False) + + def test_4(self): + self.assertEqual(is_palindrome(1234554321), True) + + def test_5(self): + self.assertEqual(is_palindrome(22), True) + + def test_6(self): + self.assertEqual(is_palindrome(1010100101), False) + + def test_7(self): + self.assertEqual(is_palindrome(1010110101), True) + + +if __name__ == '__main__': + unittest.main() From 8fb49b551a44b612e2e49c1a8f1cf8a35a43d62a Mon Sep 17 00:00:00 2001 From: Alex Botello Date: Sat, 21 Jan 2017 11:51:55 -0600 Subject: [PATCH 2/2] Refactored algorithm for Challenge 13 --- challenge_13/python/alexbotello/README.md | 14 +++++--------- challenge_13/python/alexbotello/src/palin.py | 18 ++++++++---------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/challenge_13/python/alexbotello/README.md b/challenge_13/python/alexbotello/README.md index bdf6fd288..caf15d5f2 100644 --- a/challenge_13/python/alexbotello/README.md +++ b/challenge_13/python/alexbotello/README.md @@ -2,22 +2,18 @@ ###### Language Version (Python 3.6.0) -We have our function take the integer input and convert it into a list of -integers. We find the number of digits, the midpoint, and then separate both -halves of the list. +First, we initialize a int variable ```reverse``` and set it equal to zero. As +well as make a copy variable of the integer input ```num2```. Using a while +loop, our algorithm will continue until ```num2``` is equal to zero. -If the number of digits is even, reverse the second half and compare it to the -first. If number of digits are odd, follow the same steps, but be sure to add +1 -to the index of the first half. - -Run ```palin.py`` to test your implementation +Run ```palin.py``` to test your implementation ``` 456070654 True ``` -Run ```tests.py`` to check against unit test +Run ```tests.py``` to check against unit test ``` ....... ---------------------------------------------------------------------- diff --git a/challenge_13/python/alexbotello/src/palin.py b/challenge_13/python/alexbotello/src/palin.py index 914acc19d..6e86b888a 100644 --- a/challenge_13/python/alexbotello/src/palin.py +++ b/challenge_13/python/alexbotello/src/palin.py @@ -4,20 +4,18 @@ def is_palindrome(num): """ Returns True if integer is a Palindrome """ - num = [int(x) for x in str(num)] - digits = len(num) - mid = digits // 2 - back_half = num[mid:] + reverse = 0 + num2 = num - if digits % 2 == 0: - return True if num[:mid] == back_half[::-1] else False - - else: - return True if num[:mid+1] == back_half[::-1] else False + while num2 > 0: + # Finds the last digit of num2, removes it, and adds the + # digit to the reverse variable + reverse = (10*reverse) + num2%10 + num2 //= 10 + return True if reverse == num else False if __name__ == "__main__": - num = int(input()) print() print(is_palindrome(num))