Page 1 of 3
[Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:07 pm
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.
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:09 pm
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";
?>

Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:29 pm
by Benjamin
I have completed the challenge with 3 lines of code. Should I post the code?
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:30 pm
by mikemike
Go ahead
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:33 pm
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 />";
}
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:40 pm
by tekkenlord
it worked

- i am new to php, so i dont understand it but it works
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:40 pm
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!
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:41 pm
by mikemike
I imagine mine's substantially faster
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 6:49 pm
by tekkenlord
do you mean faster to process?
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 7:03 pm
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
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 8:13 pm
by tekkenlord
i know what imploding is

lol - i probably sound so dumb to you smart php people LOL
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 8:52 pm
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?
Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 10:40 pm
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)));

Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 11:04 pm
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

Re: [Challenge] Snakes and ladder style board
Posted: Fri Aug 28, 2009 11:10 pm
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...