I'm working on certain PHP-skill-teaser. No background is really needed as error is purely PHP-based. The PasteBin.

I have double do-while in there, however I can't modify variables from within that loop, they simply don't affect the variable anyhow. I have tried a lot of solutions, including those about $GLOBALS and global $x, $y, $z; definitons within the scope. Many other solutions on similiar problems went about foreach() which didn't apply in my situation :|

But to no avail.

Writing Code: 30 minutes.
Bug Fixing: 3 hours.

#justCodersThing()

Recommended Answers

All 6 Replies

Please show ALL of your PHP code!

This is ALL of the PHP code. At least the part that I write.

Something like this will fit?

<?php

    $tests = file("php://stdin", FILE_IGNORE_NEW_LINES);

    foreach($tests as $test)
    {
        for($i = 0; $i <= $test; $i++)
        {
            $size   = 9283412;
            $isEven = (($test % 2) != 0 ? true : false);

            if($test == 0)
            {
                $size = 1;
                break;
            }

            elseif($isEven)
                $size *= 2;

            else
                $size += 1;
        }

        print $size . PHP_EOL;
    }

Giving an input file:

php script.php < data.txt

With a number each line:

1
2
3
0
4
17

Returns:

18566824
9283413
18566824
1
9283413
18566824

Is this you're trying to accomplish?

Besides, you can get the same results by avoiding the inner loop:

foreach($tests as $test)
{
    $size   = 9283412;
    $isEven = (($test % 2) != 0 ? true : false);

    if($test == 0)
        $size = 1;

    elseif($isEven)
        $size *= 2;

    else
        $size += 1;

    print $size . PHP_EOL;
}

Because $test ($_tmp in your script), it never changes, for example: if even it will be even for all his loops.

commented: Solution. +4

Here's the challenge, may shine more light.

Nice that you provided answer, but there's not a lot I can learn from that :|

The script provided, has to solve two test-cases, it did solve the first, it didn't solve the second :|, because of missing loop.

Still trying to get your first suggestion to work.

Nevermind, cereal solved issue of value not printing due to scopes. Now I can modify script to print my values, which it does, they're not correct yet, but I'll do it myself.

Thanks cereal.

Ops! I'm sorry, I didn't noticed it was a challenge... o_o'
Now that I see that link I understand... I solved this some time ago... :D

I can say that defining the type helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.