Page 2 of 3

Re: [Challenge] Snakes and ladder style board

Posted: Fri Aug 28, 2009 11:54 pm
by requinix
For the fun of it, here's the benchmarks I have. Times below are averages over 20K iterations including peak memory usage for all iterations[1].

Code: Select all

astions             : 0.00032996s  7027 KB
astions2            : 0.00011592s  7027 KB
mikemike            : 0.00006905s  6047 KB
tasairis            : 0.00006718s  6048 KB
tasairis_loopless1  : 0.00008440s  6038 KB[2]
tasairis_loopless2  : 0.00056466s  6074 KB[2]
johncartwright      : 0.00010650s  6222 KB[3]
pytrin_loopless     : 0.00009284s  7019 KB
The numbers are a bit different because I rearranged the benchmarking code. Should only measure a function call and the actual work now.

[1] Memory usage is proportional to the number of iterations so it seems there are memory leaks somewhere. Use this figure for comparisons between methods only.
[2] Includes simple modifications so that create_function is not called multiple times.
[3] Doesn't do the back-and-forth thing.

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 1:59 am
by Eran
what about john's solution? how does it perform?

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 2:32 am
by requinix
pytrin wrote:what about john's solution? how does it perform?
I thought it missed the snakes-and-ladders-style requirement...

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));

Code: Select all

100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
Regardless,

Code: Select all

johncartwright      : 0.00010650s  6222 KB

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 8:50 am
by mikemike
Good job guys :)

I'll come up with a new (harder) one soon.

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 9:38 am
by Benjamin
LOL That's great!

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 10:09 am
by Benjamin
John Cartwright wrote:

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));
It doesn't meet the challenge criteria though :(

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 1:42 pm
by John Cartwright
Thats what I get for thinking I do not need to test :)

Here is my updated version, and the more I think about it, you've actually got it wrong. The Starting point is actually on the left.

If you want the "correct version",

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, 1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 2:43 pm
by McInfo
Yet another solution:

Code: Select all

<?php
header('Content-Type: text/plain');
for ($i = 100; $i > 0; $i -= 20) {
    echo implode(' ', range($i, $i - 9)), PHP_EOL
       , implode(' ', range($i - 19, $i - 10)), PHP_EOL;
}
?>
And another:

Code: Select all

for ($i = 100; $i > 0; $i -= 20) {
    for ($j = $i; $j > $i - 10; $j--) {
        echo $j, ' ';
    }
    echo PHP_EOL;
    for ($k = $i - 19; $k < $i - 9; $k++) {
        echo $k, ' ';
    }
    echo PHP_EOL;
}
I'm not sure how we're calculating "7 lines".

Edit: This post was recovered from search engine cache.

Re: [Challenge] Snakes and ladder style board

Posted: Sat Aug 29, 2009 3:42 pm
by requinix
John Cartwright wrote:

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, -1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));
So close... It's that 91-100 line:

Code: Select all

91 92 93 94 95 96 97 98 99 100
81 82 83 84 85 86 87 88 89 90
If you change the substr to use 0,-1 instead of 0,1 it's perfect.

Code: Select all

with loops
astions1  : 0.00032996s  7027 KB
astions2  : 0.00011592s  7027 KB
mikemike  : 0.00006905s  6047 KB
tasairis  : 0.00006718s  6048 KB
mcinfo1   : 0.00006479s  6247 KB
mcinfo2   : 0.00005899s  6450 KB
 
without loops
tasairis1       : 0.00008440s  6038 KB[2]
tasairis2       : 0.00056466s  6074 KB[2]
pytrin          : 0.00009284s  7019 KB
johncartwright  : 0.00014869s  6230 KB[2]
[2] Includes simple modifications so that create_function is not called multiple times.

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 2:47 am
by VladSun
A recursive, loopless one :)

Code: Select all

function gen($start, $end, $reverse = 0)
{
    if ($start > 0)
        return (
            implode(" ",
                range(
                    $start - $reverse*20 + $reverse,
                    $end - !$reverse*20 + 20 + !$reverse
                )
            ).
            "\r\n".
            gen($start - $reverse*20, $end - !$reverse*20, !$reverse)
        );
}
echo gen(100, 90);
=>

Code: Select all

function gen($start, $end, $reverse = 0)
{
    if ($start > 0)
        return (implode(" ", range($start - $reverse*20 + $reverse, $end - !$reverse*20 + 20 + !$reverse))."\r\n".gen($start - $reverse*20, $end - !$reverse*20, !$reverse));
}
echo gen(100, 90);
PS: Sorry for the delay, I was on vacation :)

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 2:49 am
by Benjamin
Ah decomposition in action :)

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 3:00 am
by VladSun
mikemike wrote:Print list of numbers from 1-100. These numbers should be in rows of 10 and in a zig-zag fashion.
First I thought you meant the Z-scan used in JPEG format - http://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg :)
Maybe it could be a good challenge too :)

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 3:57 am
by onion2k
astions wrote:I was trying to be cool by decreasing the number of lines which compromised performance.
I think that's a valid goal, but I also think solutions for optimised performance, and for optimised readability are valid too. These Challenges should be about thinking up new and interesting approaches to problems, regardless of how they're interesting.

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 4:09 am
by onion2k
Just for completeness, here's my solution. It should be pretty quick considering it's not doing very much.

Code: Select all

<style>
#board {
    float: left;
    width: 600px;
    height: 600px;
    border: 10px solid #888;
}
.square {
    width: 60px;
    height: 60px;
    color: #888;
}
.l {
    float: left;
}
.r {
    float: right;
}
</style>
 
<div id="board">
 
<?php
 
    for ($x=100;$x>0;$x--) {
 
        if ($x%10==0) {
            $dir = ($c++%2==0) ? "l" : "r";
        }
        
        echo "<div class=\"square ".$dir."\">".$x."</div>";
        
    }
    
?>
 
</div>

Re: [Challenge] Snakes and ladder style board

Posted: Tue Sep 01, 2009 5:28 am
by Benjamin
Very nice onion2k, I didn't think to create it using css.

Might want to put an @ sign in front of the $c ;)