Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is one of codeeval challenges

Challenge Description:

Credits: Programming Challenges by Steven S. Skiena and Miguel A. Revilla

The problem is as follows: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure. E.g.

195 (initial number) + 591 (reverse of initial number) = 786

786 + 687 = 1473

1473 + 3741 = 5214

5214 + 4125 = 9339 (palindrome) In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.

Here is my solution for it:

#!/usr/bin/env python
import sys

"""
I've done some tests with timeit and it seems that both numeric and string version
have the same performance (at least for numbers < 10000)

# def reverse_num(num):
#   rev = 0
#   while(num > 0):
#     rev = (10*rev)+num%10
#     num //= 10
#   return rev
"""

def reverse(num):
    """Reverses the number

       >>> reverse(1456)
       6541
       >>> reverse(111)
       111
    """
    return int(str(num)[::-1])

def palindrome(num):
    """Return the number of iterations required to compute a palindrome

       >>> palindrome(195)
       (4, 9339)
    """
    # compute in 100 iterations or less
    for i in range(100):
        rnum = reverse(num)
        if rnum == num:
            break
        num = num + rnum
    return (i, num)

if __name__ == "__main__":
    with open(sys.argv[1]) as f:
        for line in f:
            print "%s %s" % palindrome(int(line))

Any remarks on the code style, on the algorithm itself?

share|improve this question
    
Two comments: Probably nice to error-handle cases where it blew past a 100 iterations (vs. finishing exactly on iteration 100! :)) Also for curiosity's sake, did you re-write palindrome(num) using recursion to see if there would be a performance hit and by how much? –  shivsky Oct 28 '13 at 16:18

1 Answer 1

Only 2 things:

  1. Why do you have this? This makes your code a bit unclear and confusing. Remove this if you do not want to use it as it might make your actual code look messy.

    # def reverse_num(num):
    #   rev = 0
    #   while(num > 0):
    #     rev = (10*rev)+num%10
    #     num //= 10
    #   return rev
    
  2. It is discouraged to use for i in range(100): in Python.

share|improve this answer
    
I assume that your comment in 2 indicates that using enumerate was more idiomatic? –  shivsky Nov 4 '13 at 16:29
2  
for i in range(100) is in good use here. Could be xrange instead on Python 2, though. –  Janne Karila Nov 20 '13 at 13:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.