crazy question

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

crazy question

Post 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.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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>";
    
}
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

wiseguy :lol:
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Come on now, we want to see your code :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You could do that with a recursive function too ;)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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";
;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply