HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

The finest integers are palindromes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thepalindromesarefinestintegers

Problem

Challenge:


Reverse the digits of a number and add it to the original
until the number is a palindrome

Specifications:


Your program should accept as its first argument a path to a filename.

Each line in this file is one test case containing an integer n

  • General feedback on good vs poor practices in my code.

Solution


  • Indentation and formatting


currentNum = Integer.parseInt(line)
;

  • Is there a chance of an MAX_INT overflow for reverse(). Seems unlikely for the given contraints though.



  • Since n is an integer, so negatives are allowed. In that case .reverse() will cause problem. Please check the constraints.



  • getPalindrome() should return a palindrome instead of an output you wish to have. Instead either have the function name changed. (Though not a big concern but ensure that when I see a function name, it shoul dbe intuitive so that I do not have to check the code for the actual output.)



  • Test cases:



  • Include 0, 9998 (max non palindromic n).



  • Negatives (if constraints dont mention so).



  • Invalid input. (Not handled by your program and may not be required but would help for a production code and not a programming challenge).



Optimisation:

-
reverse() though takes negligible time (in nanoseconds) but can be improved to handle all above cases along with speed in calculation. Ideone link

private static int reverseInt(int n) {
    int rev = 0;
    while(n>0){
        rev = (rev*10)+n%10;
        n=n/10;
   }
   return rev;
}

Code Snippets

private static int reverseInt(int n) {
    int rev = 0;
    while(n>0){
        rev = (rev*10)+n%10;
        n=n/10;
   }
   return rev;
}

Context

StackExchange Code Review Q#78944, answer score: 6

Revisions (0)

No revisions yet.