Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find test-case for which code does't work

Tags:

java

I submitted one code in code chef but it's giving wrong answer even if it's correct can anybody help me to identify that please. I have tried so many inputs and calculated manually and they are correct so why they gave me wrong answer. so,anybody who can find the TEST Case which give incorrect output by this code ?.

Here is Problem definition. Code definition

import java.util.Scanner;
import java.lang.Math;
class Codechef {

    static int get(int n,int i,int digit)
    {
        int p;
        p=(int)Math.pow(10,i-1);
        n=n/p;
        return n%10;
    }

    static boolean check_pal(int n)
    {
        int digit;
        digit=(int) (Math.log10(n)+1);
        int a=0,b=0,i,j,p;
        int sum=0;

        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }

        if(sum==n)
            return true;
        else
            return false;
    }

    static int reverse(int n)
    {
        int digit;
        digit=(int) (Math.log10(n)+1);
        int a=0,b=0,i,j,p;
        int sum=0;

        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }

        return n+sum;
    }

    public static void main(String[] args) {
        try{
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();

            if(n<10 || n>999){
                System.out.println("NONE");   
                return;
            }

            boolean c;
            for(int i=1 ; i<=100 ; i++)
            {
                c=check_pal(n);
                if(c==true)
                {
                    System.out.println(n);
                    return;
                }
                n=reverse(n);
            }
            System.out.println("NONE");
        }
        catch(Exception e)
        {
            System.out.println("NONE"); 
        }
    }
}

Here is one more output. for 99 it gives 99 and which is correct as it's palindrome. for 99


2 Answers

For 89 (or 98 for that matter), your code returns "NONE", although you reach the answer 8813200023188 after only 24 steps.

Another case is that for 177 and 276 you should get 8836886388 instead of NONE

I didn't debug your code, I just wrote a program that does the same, and compared the output my program gave to the one your program gave. Since you just requested a testcase, that should suffice :) My gutfeeling is that you overflow... an int is not large enough to hold the answer in all cases.

Happy bughunting.

Edit (on Request) with my code.

I didn't change your code, except that I extracted your logic into a getResult(integer) methode so that I could bypass the scanning of the input and simply return a string as result. It prints out all the differences between our versions. I used BigInteger as the type to hold my results.

public class Main {

public static void main(String[] args) {
    Main m = new Main();
    for (int i=10; i < 1000; i++) {
        String myResult = null;
        String hisResult = null;
        try {
        myResult = m.getResultAsString(i);
        } catch (Exception e){
        System.out.println("Your code threw an exception for " + i);
        }
        try{
            hisResult = Codechef.getResult(i);
        } catch (Exception e){
            System.out.println("His code threw an exception for " + i);
        }
        if (myResult != null && hisResult != null && ! myResult.equals(hisResult)) {
            System.out.println("For " + i + "  you have " + myResult + " but he has " + hisResult);
        }
    }
}

public String getResultAsString(int inputNumber) {
    BigInteger res = getResultAsBigInteger(new BigInteger(""+inputNumber));
    if (res != null) {
        return res.toString();
    } else {
        return "NONE";
    }

}

public BigInteger getResultAsBigInteger(BigInteger inputNumber) {
    int numberOfSteps = 0;
    BigInteger currentValue = inputNumber;
    while (numberOfSteps < 101 && ! isPalindrome(currentValue)) {
        numberOfSteps++;
        currentValue = currentValue.add(reverseDigits(currentValue));
    }
    return numberOfSteps < 101 ? currentValue : null;
}

public boolean isPalindrome(BigInteger number) {
    return number.equals(reverseDigits(number));
}

public BigInteger reverseDigits(BigInteger input) {
    String inputString = input.toString();
    String output = "";
    for (int i = inputString.length() - 1; i >= 0; i--)
    {
        output += inputString.charAt(i);
    }
    return new BigInteger(output);
}

}

like image 69
Yves V. Avatar answered Jul 04 '26 09:07

Yves V.


There is an overflow error in your code. for input 89 it's not working as @Yves V. said Suggestion is to use BigInteger class of lang.Match it will be useful to eliminate this overflow error.

like image 32
CodeLover Avatar answered Jul 04 '26 09:07

CodeLover