Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Challenge 13 (Pending) #420

Merged
merged 3 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactored algorithm for Challenge 13
  • Loading branch information
alexbotello committed Jan 21, 2017
commit 8fb49b551a44b612e2e49c1a8f1cf8a35a43d62a
14 changes: 5 additions & 9 deletions challenge_13/python/alexbotello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
.......
----------------------------------------------------------------------
Expand Down
18 changes: 8 additions & 10 deletions challenge_13/python/alexbotello/src/palin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))