samedi 5 septembre 2020

Need help comparing two positive integers

What I'm trying to accomplish

I'm trying to learn PHP by solving exercises in codility, I'm trying to solve the binary gap one, which is my first program in PHP ever.

This is the code I've written

function solution($N) {
    return (getLongestBinaryGap(getBinary($N)));
}

function getLongestBinaryGap($bin)
{
    //100101 -> 1...1
    $itIsAGap = False;
    $currGap = 0;
    $maxGap = 0;


    while($bin > 1)
    {
        $aux = intdiv($bin, 10);
        printf("\$bin = %d\n", $bin);
        printf("\$aux * 10 = %d\n", $aux*10);
        if ($aux * 10 == $bin)
        {
            printf("I enter if the numbers ends with a 0\n");
            if ($itIsAGap)
            {
                $currGap++;
                printf("\$currGap = %d\n", $currGap);
            }
        }
        else
        {
            if($maxGap < $currGap)
            {
                $maxGap = $currGap;
            }
            $currGap = 0 ;
           
            if ($bin >= 11)
            {
                print("itIsAGap\n");
                $itIsAGap = True;
            }
        }
        $bin /= 10;
        //10001001 -> 1000100 -> 100010 -> 10001 -> 1000 -> 100 -> 10 -> 1
    }
    return $maxGap;
}

function getBinary($num) // Works OK
{
    $ans = 0;
    while($num > 0)
    {
        $bin = 1 ;
        for ( $i = 1 ; $i <= $num ; $i *= 2 )
        {
            $bin *= 10;
            $acum = $i; 
        }
        $bin /= 10;
        $ans += $bin;
        $num -= $acum;
    }
    return $ans;
}

It works OK if the original number ends with a 0 in the beginning, but it doesn't when the original number ends with a 1, it's like the line $aux = intdiv($bin, 10); converts the $aux variable into a different type of variable than $bin, for some reason, and the program doesn't realize that, for example, if $aux = 1*10 and $bin = 10 then $aux == $bin anymore...


I have a huge problem with this line:

if ($aux * 10 == $bin)

EDIT: Sorry for the missing output, here it goes:

Test Output
Test Output
Compilation successful.

Example test:   1041
Output:
$bin = 10000010001
$aux * 10 = 10000010000
itIsAGap
$bin = 1000001000
$aux * 10 = 1000001000
itIsAGap
$bin = 100000100
$aux * 10 = 100000100
itIsAGap
$bin = 10000010
$aux * 10 = 10000010
itIsAGap
$bin = 1000001
$aux * 10 = 1000000
itIsAGap
$bin = 100000
$aux * 10 = 100000
itIsAGap
$bin = 10000
$aux * 10 = 10000
itIsAGap
$bin = 1000
$aux * 10 = 1000
itIsAGap
$bin = 100
$aux * 10 = 100
itIsAGap
$bin = 10
$aux * 10 = 10
$bin = 1
$aux * 10 = 0
WRONG ANSWER (got 0 expected 5)

Example test:   15
Output:
$bin = 1111
$aux * 10 = 1110
itIsAGap
$bin = 111
$aux * 10 = 110
itIsAGap
$bin = 11
$aux * 10 = 10
itIsAGap
$bin = 1
$aux * 10 = 0
OK

Example test:   32
Output:
$bin = 100000
$aux * 10 = 100000
I enter if the numbers ends with a 0
$bin = 10000
$aux * 10 = 10000
I enter if the numbers ends with a 0
$bin = 1000
$aux * 10 = 1000
I enter if the numbers ends with a 0
$bin = 100
$aux * 10 = 100
I enter if the numbers ends with a 0
$bin = 10
$aux * 10 = 10
I enter if the numbers ends with a 0
OK

Producing output might cause your solution to fail performance tests.
You should remove code that produces output before you submit your solution.

Detected some errors.

Aucun commentaire:

Enregistrer un commentaire