forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#420 from alexbotello/master
[Python] Challenge 13 (reviewed and merged)
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Integer Palindrome | ||
###### Language Version (Python 3.6.0) | ||
|
||
|
||
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. | ||
|
||
Run ```palin.py``` to test your implementation | ||
``` | ||
456070654 | ||
True | ||
``` | ||
|
||
Run ```tests.py``` to check against unit test | ||
``` | ||
....... | ||
---------------------------------------------------------------------- | ||
Ran 7 tests in 0.001s | ||
OK | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Integer Palindrome | ||
|
||
def is_palindrome(num): | ||
""" | ||
Returns True if integer is a Palindrome | ||
""" | ||
reverse = 0 | ||
num2 = num | ||
|
||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |