[Challenge] Snakes and ladder style board

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

[Challenge] Snakes and ladder style board

Post by mikemike »

This is fairly easy, I'll post a hard one tomorrow if I get chance.

Print list of numbers from 1-100. These numbers should be in rows of 10 and in a zig-zag fashion, like a snakes and ladders board:

Code: Select all

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

The code should be no more than 7 lines (excluding comments and <?php ?> tags). And no cheating and just removing line breaks, one command/brace per line. I have my 7 line solution here ready.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: [Challenge] Snakes and ladder style board

Post by mikemike »

Oh, and none of this either:

Code: Select all

<?php
echo "100 99 98 97 96 95 94 93 92 91\n81 82 83 84 85 86 87 88 89 90\n80 79 78 77 76 75 74 73 72 71\n61 62 63 64 65 66 67 68 69 70\n60 59 58 57 56 55 54 53 52 51\n41 42 43 44 45 46 47 48 49 50\n40 39 38 37 36 35 34 33 32 31\n21 22 23 24 25 26 27 28 29 30\n20 19 18 17 16 15 14 13 12 11\n1 2 3 4 5 6 7 8 9 10";
?>
;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

I have completed the challenge with 3 lines of code. Should I post the code?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: [Challenge] Snakes and ladder style board

Post by mikemike »

Go ahead
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

Code: Select all

 
<?php
for ($i = 0; $i < 10; $i++) {
    echo implode(" ", ($i % 2 != 0) ? array_reverse(array_slice(range(100,1), $i * 10, 10)) : array_slice(range(100,1), $i * 10, 10)) . "<br />";
}
 
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: [Challenge] Snakes and ladder style board

Post by tekkenlord »

it worked :) - i am new to php, so i dont understand it but it works
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: [Challenge] Snakes and ladder style board

Post by mikemike »

Very nice.

My 7-line solution was:

Code: Select all

for($c=-1,$i=100;0< $i;$i+=$c)
  if (($i-($c<0))%10)
    echo $i,' ';
  else {
    echo $i,"\n";
    $i-=10+$c=-$c;
  }
 
Not sure who's is faster though, can't be bothered to benchmark right now either to be honest!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: [Challenge] Snakes and ladder style board

Post by mikemike »

I imagine mine's substantially faster
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: [Challenge] Snakes and ladder style board

Post by tekkenlord »

do you mean faster to process?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Challenge] Snakes and ladder style board

Post by requinix »

Mine's similar to astions. Kinda.

Code: Select all

for ($i = 100; $i >= 10; $i -= 10) {
    echo implode(" ", range(($i % 20 ? $i - 9 : $i), ($i % 20 ? $i : $i - 9))) . "\n";
}
And I did some benchmarking. With output buffering and 10K iterations, average times on my machine were

Code: Select all

astions   : 0.00033084
mikemike  : 0.00006718
tasairis  : 0.00007377
tekkenlord
Forum Newbie
Posts: 11
Joined: Fri Aug 28, 2009 6:21 pm

Re: [Challenge] Snakes and ladder style board

Post by tekkenlord »

i know what imploding is :D lol - i probably sound so dumb to you smart php people LOL
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

I was trying to be cool by decreasing the number of lines which compromised performance. I'm not sure how much better this will perform, but it should be quite a bit faster:

Code: Select all

 
<?php
$numbers = range(100,1);
for ($i = 0; $i < 10; $i++) {
    echo implode(" ", ($i % 2 != 0) ? array_reverse(array_slice($numbers, $i * 10, 10)) : array_slice($numbers, $i * 10, 10)) . "<br />";
}
 
Now... how about we write this without using a for or while loop construct?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: [Challenge] Snakes and ladder style board

Post by John Cartwright »

astions wrote:I was trying to be cool by decreasing the number of lines which compromised performance. I'm not sure how much better this will perform, but it should be quite a bit faster:

Code: Select all

 
<?php
$numbers = range(100,1);
for ($i = 0; $i < 10; $i++) {
    echo implode(" ", ($i % 2 != 0) ? array_reverse(array_slice($numbers, $i * 10, 10)) : array_slice($numbers, $i * 10, 10)) . "<br />";
}
 
Now... how about we write this without using a for or while loop construct?

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));
:)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] Snakes and ladder style board

Post by Eran »

Here's mine without for/while:

Code: Select all

$numbers = range(10,1);
array_walk($numbers,'snake');
function snake($val) {
    $row = range(10 * $val, (10 * $val) - 9);
    $val % 2 == 1 ? asort($row) : false;
    echo implode(' ',$row) . '<br />';
}
EDIT: nice one, John :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Challenge] Snakes and ladder style board

Post by requinix »

astions wrote:Now... how about we write this without using a for or while loop construct?
Did it a couple different ways.

Takes 10 through 1 and creates 100-91 through 1-10

Code: Select all

echo implode("\n",
    array_map(create_function('$n', '
        return implode(" ", range(
            $n % 2 ? 10 * ($n - 1) + 1 : 10 * $n,
            $n % 2 ? 10 * $n : 10 * ($n - 1) + 1
        ));
    '), range(10, 1))
);
Takes 100 through 1 and "reverses" some number ranges (eg, 90-81)

Code: Select all

echo implode("", array_map(create_function('$n', '
    return $n . ((floor($n / 10) % 2) && ($n % 10 <= 1) ? "\n" : " ");
'), array_map(create_function('$n', '
    return (floor(--$n / 10) % 2 ? $n + 1 : 10 * floor($n / 10) - ($n % 10) + 10);
'), range(100, 1))));
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)));
Umm...
Post Reply