0

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()

Edited by Aeonix

3
Contributors
6
Replies
28
Views
1 Year
Discussion Span
Last Post by cereal
0

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

1

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.

Edited by cereal

Votes + Comments
Solution.
0

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.

Edited by Aeonix

0

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.

0

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.

Edited by cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.