An Interesting Problem
by Mike on May.07, 2009, under Web Development
Myself and another developer have begun work on a new project that will integrate into Twitter. It’s quite a novel idea and I’m very surprised it hasn’t been done already, but there will be more on that once it’s ready for public use.
But enough on that, this post is about a problem we came across when building it. Basically we wanted to create a block of 100 cells, each numbered, but the rows are to go in a zigzag fashion, starting in the bottom left at 0 and ending in top left with 100, and obviously this isn’t to be done manually. Find an example below:
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
Here is what I came up with (PHP):
$i=99;
for ($x = 100; $x > 0; $x--)
{
echo $x . ' ';
if($i%10==0) {
// Display new line
echo "\n";
// Loop through 10
for($y = ($x-10); $y < ($x); $y++){
echo $y . ' ';
if($y%10==0) echo "\n";
}
// Add ten onto $x and $i
$x = $x-10;
$i = $i-10;
}
$i--;
}
Although this is relatively efficient, I'm sure there must be a better way of doing it. Any ideas? Leave a comment.
May 7th, 2009 on 8:02 pm
// One loop
$c = true;
for($a = 100, $b = 0; $a > 0; –$a, ++$b)
{
if ($c) echo $a. ‘ ‘;
else echo $a + (($b % 10)*2 – 9). ‘ ‘;
if (($a-1) % 10 == 0) {
$c = !$c;
echo “\n”;
}
}
/* ==================================================== */
// Loops
for($i = 10; $i > 0; –$i) {
if ($i % 2 == 0) {
for($j = 10; $j > 0; –$j)
echo ($i*10 + $j – 10). ‘ ‘;
} else {
for($j = 0; $j < 10; ++$j)
echo ($i*10 + $j – 9). ‘ ‘;
}
echo “\n”;
}
I’d do it something like that, probably about as efficient as each other.
May 7th, 2009 on 8:43 pm
Compacted version:
for($a=100,$b=0,$c=0;$a>0;--$a,++$b){echo(($c)?$a:$a+(($b % 10)*2-9)).' ';if(($a-1)%10==0){$c=!$c;echo"\n";}}
=)
May 7th, 2009 on 8:46 pm
Compacted version: – Posted it incorrectly first time
for($a=100,$b=0,$c=0;$a>0;--$a,++$b){echo(($c)?$a+(($b % 10)*2-9):$a).' ';if(($a-1)%10==0){$c=!$c;echo"\n";}}
May 7th, 2009 on 11:48 pm
<?php
echo “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″;
?>
1 line of code.
May 8th, 2009 on 12:43 am
low pal
0; $i–) {
echo $i . ” “;
if ($br == 10) {
echo “”;
$br = 0;
}
$br++;
}
?>
as seen @ http://www.buccs.co.uk/mike.php
May 8th, 2009 on 12:44 am
woohoo for stripping!!
http://www.buccs.co.uk/mike.phps for source!!
May 8th, 2009 on 2:23 am
As always Jo, your comments are invaluable.
May 8th, 2009 on 2:24 am
Pete – That’s not what my code does, the numbers need to reverse after each row – hence the double loop
May 8th, 2009 on 8:41 am
and ignore my “entry” as i realised what you need lol
May 9th, 2009 on 3:17 am
It’s ugly, but hey –
0;$i+=$c)
if (($i-($c<0))%10)
echo $i,’ ‘;
else {
echo $i,”\n”;
$i-=10+$c=-$c;
}
May 9th, 2009 on 3:18 am
Tag stripping, eh?
)
for($c=-1,$i=100;0< $i;$i+=$c) if (($i-($c<0))%10) echo $i,' '; else { echo $i,"\n"; $i-=10+$c=-$c; }