Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Non-ending loop

     

SoulMaster

12:42 am on Jan 12, 2009 (gmt 0)

5+ Year Member



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:55 am on Jan 12, 2009 (gmt 0)

10+ Year Member



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

2:34 pm on Jan 12, 2009 (gmt 0)

5+ Year Member



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

7:03 pm on Jan 12, 2009 (gmt 0)

5+ Year Member



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];

}

 

Featured Threads

Hot Threads This Week

Hot Threads This Month