Skip to content

Commit

Permalink
Refactored algorithm for Challenge 13
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbotello committed Jan 21, 2017
1 parent 1c5e94a commit 8fb49b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
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))

0 comments on commit 8fb49b5

Please sign in to comment.