<?php
$eggs = array(1,1,1,1,1,2,1,1,1,1,1,1);
$eggs1_6 = array_slice($eggs, 0, 6); $eggs7_12 = array_slice($eggs, 6, 6);
if (array_sum($eggs1_6) > array_sum($eggs7_12)) $half_eggs = $eggs1_6; else $half_eggs = $eggs7_12;
$eggs1_3 = array_slice($half_eggs, 0, 3); $eggs4_6 = array_slice($half_eggs, 3, 3);
if (array_sum($eggs1_3) > array_sum($eggs4_6)) $quarter_eggs = $eggs1_3; else $quarter_eggs = $eggs4_6;
if ($quarter_eggs[0] == $quarter_eggs[1]) $egg = $quarter_eggs[2]; else if ($quarter_eggs[0] > $quarter_eggs[1]) $egg = $quarter_eggs[0]; else $egg = $quarter_eggs[1];
$index = array_search($egg, $eggs) + 1;
print "Heavy egg is egg number {$index}\n";
?>
murugesan wrote:
In a group of 12 eggs only one is having weight less or more. Can we have a program to find the egg, provided we have to weigh only three times with a weighing machine with two pans,without using weights.
-murugesan ----- Original Message ----- From: "Matt Matijevich" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, August 29, 2003 2:55 AM Subject: RE: [PHP] PHP Interview questions
<snip> Can you do it programmitically (with pseudo-code)? After all, we need to keep this on-topic :) </sip>
not pseudo code and it could definatley be cleaned up or done better but her is my quick try at it
echo "We are walking to up to a fork in the road.<br />"; $randval = rand(1, 500); $randval2 = rand(1, 500); $randval3 = rand(1, 500);
if ( $randval&1 ) { $man1 = "truth"; $man2 = "lie";
} else { $man1 = "lie"; $man2 = "truth"; }
if ( $randval3&1 ) { $road1 = "Utpoia"; $road2 = "Black Forest";
} else { $road1 = "Black Forest"; $road2 = "Utpoia"; }
if ( $randval2&1 ) $manToAsk = "Man 1"; else $manToAsk = "Man 2";
if ($manToAsk == "Man 1") { echo "We are asking Man 1<br />Man 1, where would Man 2 point if I asked him to point to the road to Utopia?<br />"; if (man1() == "Road 1") { echo "Man 1 pointed to Road 1<br />"; $roadToTake = "Road 2"; } else { echo "Man 1 pointed to Road 2<br />"; $roadToTake = "Road 1"; } } else { echo "We are asking Man 2<br />Man 2, where would Man 1 point if I asked him to point to the road to Utopia?<br />"; if (man2() == "Road 1") { echo "Man 2 pointed to Road 1<br />"; $roadToTake = "Road 2"; } else { echo "Man 2 pointed to Road 2<br />"; $roadToTake = "Road 1"; } }
echo "We are taking $roadToTake<br />Road 1 goes to $road1<br />Road 2 goes to $road2<br />Man 1 = $man1<br />Man 2 = $man2"; //echo "We took $roadToTake<br />We asked $manToAsk<br />Man 1 = $man1<br />Man 2 = $man2<br />Road 1 goes to $road1<br />Road 2 goes to $road2";
function man1() { global $man1,$man2,$road1,$road2; if ($man1 != "truth") { if ( $road1 == 'Black Forest' ) return 'Road 1'; else return 'Road 2'; } else { if ( $road1 == 'Black Forest' ) return 'Road 1'; else return 'Road 2'; } }
function man2(){ global $man1,$man2,$road1,$road2; if ($man2 != "truth") { if ( $road1 == 'Black Forest' ) return 'Road 1'; else return 'Road 2'; } else { if ( $road1 == 'Black Forest' ) return 'Road 1'; else return 'Road 2'; } }
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php