Sunday, August 30, 2015

if integer is palindrome

(n):
    if n < 0:
        n = -n
    digits = new (capacity: 10) // int.max
    while n > 0:
        digit = n %10
        n = n /10
        digits.add(digit)
    result = true
    start = 0
    end = digits.length - 1
    while start < end:
        if digits[start] != digits[end]
            result = false
            break
        start++
        end--
    return result
[Hat tip to SM]

No comments:

Post a Comment