Page 1 of 1

crazy question

Posted: Thu Nov 24, 2005 6:31 am
by shiznatix
ok i did not have that much time to figure this one out but on a java test i just had i had to write a method that would output numbers like this

111111111
122222222
123333333
123444444
123455555
123456666
123456777
123456788
123456789

but you know, with loops and stuff, the hard way. it said to only use 2 for loops but i ended up using 3 and submitted my test with literally 1 second left on the clock (whew). my question to you, how would you write that with only 2 for loops? php is fine since it would be basically the same.

Posted: Thu Nov 24, 2005 6:44 am
by Buddha443556
The sequence?

Code: Select all

// just pseudo code
for i = 1 to 9
  for j = 1 to 9
    if j =< i
      print j
    else
      print i
    endif
  next
next

Posted: Thu Nov 24, 2005 8:30 am
by Grim...
Only one loop, and the first number can contain as many ones as you want...

Code: Select all

$nums = "111111111"; //can be as many as you want
$len = strlen($nums);
print $nums."<br>";
for ($i = 1; $i < $len; $i++) 
{
    $change = substr($nums, $i, 1);
	$change ++;
    $nums = substr($nums, 0, $i);
    $nums = str_pad($nums, $len, $change);
    
    print $nums."<br>";
    
}

Posted: Thu Nov 24, 2005 8:45 am
by n00b Saibot
wiseguy :lol:

Posted: Fri Nov 25, 2005 1:14 am
by shiznatix
gah rediculous. i cant believe i didnt see it at first. i won't even post the code that i was using instead of that. :oops: at least i got credit for it.

Posted: Fri Nov 25, 2005 3:50 am
by Grim...
Come on now, we want to see your code :)

Posted: Fri Nov 25, 2005 4:12 am
by shiznatix
Its gone! I was messing around with the file on a linux box and i saved over it. Before closing it i went to get it back to the original contents before the save but i could not control+z my way back to the original code. Trust me though, it was bad. My only excuse is that I had not slept in over 26 hours and I did not know that we where going to have the test so I was not prepard at all.

Posted: Fri Nov 25, 2005 4:27 am
by foobar
shiznatix wrote:Its gone! I was messing around with the file on a linux box and i saved over it. Before closing it i went to get it back to the original contents before the save but i could not control+z my way back to the original code. Trust me though, it was bad. My only excuse is that I had not slept in over 26 hours and I did not know that we where going to have the test so I was not prepard at all.
</lousy-excuse>

Posted: Fri Nov 25, 2005 5:00 am
by shiznatix
foobar everything i say is a lie. except that, and that, and that, and that, and that...

no i am being completly honest. the eclipse editor for fedora core saves only about 20 things for you to roll back on and i am used to edit-plus2 saving like 100.

Posted: Fri Nov 25, 2005 6:34 am
by Chris Corbyn
You could do that with a recursive function too ;)

Posted: Fri Nov 25, 2005 6:55 am
by onion2k
No loops!!!

Code: Select all

echo "111111111";
echo "122222222";
echo "123333333";
echo "123444444";
echo "123455555";
echo "123456666";
echo "123456777";
echo "123456788";
echo "123456789";
;)

Posted: Fri Nov 25, 2005 6:59 am
by Chris Corbyn
onion2k wrote:No loops!!!

Code: Select all

echo "111111111";
echo "122222222";
echo "123333333";
echo "123444444";
echo "123455555";
echo "123456666";
echo "123456777";
echo "123456788";
echo "123456789";
;)
I see a flaw in your valiant attempt there :P