homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Non-ending loop
SoulMaster

5+ Year Member



 
Msg#: 3824173 posted 12:42 am on Jan 12, 2009 (gmt 0)

So basically the array $indians is array full of numbers, wich are ment to be the tallness's of indians standing in a row, and i have to find out wich indian can see most of indians in front of him. He sees the dude standing in front of him for sure, and seeing of the next guy depends on if this next guy is shorter than the indian who looks him or longer than the dude who he stands in front of.
Okay i know it's messy, but look the code, I can't figure out why

for($checkedIndian=$key-2; $indians[$checkedIndian]<$currentIndian $indians[$checkedIndian]>$indians[$checkedIndian+1]; $checkedIndian--)

Wont end. I get error:
Fatal error: Maximum execution time of 30 seconds exceeded

Here's the code:

function getHighestIndianPosition ($indians) {
$indians=array_reverse($indians, true);
foreach($indians as $key => $currentIndian){
$sees[$key]=1;
for($checkedIndian=$key-2; $indians[$checkedIndian]<$currentIndian
$indians[$checkedIndian]>$indians[$checkedIndian+1]; $checkedIndian--){
$sees[$key]++;
}
if($sees[$key]>=$key-1){
return $key;
$findHighest=1;
break;
}
}
if($findHighest!=1){
$highestSees=0;
foreach($sees as $value){
if($value>$highestSees)$highestSees=$value;
}
$sees=array_flip($sees);
return $sees[$highestSees];
}
}

The reason i reversed the array is, that the thing needs to be as fast as possible, there will be a competiton later on, who's code works the fastest.

 

Romeo

10+ Year Member



 
Msg#: 3824173 posted 10:55 am on Jan 12, 2009 (gmt 0)

You may add some debugging information to the code to analyze if the stuff works as expected:
put a few echo statements into your foreach and for loops showing you the status of your current sees-keys and checked-Indians and whatever you may find appropriate, to get a feeling if your algo computes as you expect.

Kind regards,
R.

SoulMaster

5+ Year Member



 
Msg#: 3824173 posted 2:34 pm on Jan 12, 2009 (gmt 0)

I can't cause i get no other response from the page than that 30 sec time exceeded. I think i should give you the specific $indians array too so there are no dontknows in this case for you:
$indians = array(175, 210, 169, 200, 183, 176, 163, 179, 232, 160, 142);
In this case the answer must be 9'th indian, aka the onw with index 8.

SoulMaster

5+ Year Member



 
Msg#: 3824173 posted 7:03 pm on Jan 12, 2009 (gmt 0)

Ty Rome for reply.
You gave me an ide, i chenged the code a lil and i guess its faster too now, and should work good:)


function getHighestIndianPosition ($indians) {
$indians=array_reverse($indians, true);
$highestSees=0;
foreach($indians as $key => $currentIndian){
$sees[$key]=1;
for($checkedIndian=$key-2; $checkedIndian>=0; $checkedIndian--){
if($currentIndian>$indians[$checkedIndian+1] $indians[$checkedIndian]>$indians[$checkedIndian+1])$sees[$key]++;
else break;
}
if($sees[$key]>$highestSees)$highestSees=$sees[$key];
if($sees[$key]>=$key-1 && $sees[$key]>=$highestSees){
return $key;
break;
}
}
$sees=array_flip($sees);
return $sees[$highestSees];

}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved