crazy question
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
crazy question
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.
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.
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
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
nextOnly 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>";
}- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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>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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
No loops!!!

Code: Select all
echo "111111111";
echo "122222222";
echo "123333333";
echo "123444444";
echo "123455555";
echo "123456666";
echo "123456777";
echo "123456788";
echo "123456789";- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I see a flaw in your valiant attempt thereonion2k wrote:No loops!!!
Code: Select all
echo "111111111"; echo "122222222"; echo "123333333"; echo "123444444"; echo "123455555"; echo "123456666"; echo "123456777"; echo "123456788"; echo "123456789";