Problem: Check whether a whole number is palindrome or not.
Solution in Python:
import math
def check_for_palindrome_number(n):
"""
Author: Mayur P Srivastava
"""
assert n >= 0
n_digits = int(math.floor(math.log10(n)))
upper_mult = 10 ** n_digits
lower_mult = 10
while lower_mult <= upper_mult:
lower = (n % lower_mult) // (lower_mult // 10)
upper = (n // upper_mult) % 10
if lower != upper:
return False
lower_mult *= 10
upper_mult //=10
return True
Concepts Learned: Single Loops, numbers.
No comments:
Post a Comment