This is a discussion on RE: [PHP] PHP Interview questions within the PHP General forums, part of the PHP Programming Forums category; <snip> Can you do it programmitically (with pseudo-code)? After all, we need to keep this on-topic :) &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
<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'; } } |
|
|||
|
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" <matijevich@alliancetechnologies.net> To: <php-general@lists.php.net> 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 > |
|
|||
|
murugesan, et al --
...and then murugesan said... % % 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. *Without* a balance scale? Hmmm... That doesn't sound familiar to me... % % -murugesan HAND :-D -- David T-G * There is too much animal courage in (play) davidtg@justpickone.org * society and not sufficient moral courage. (work) davidtgwork@justpickone.org -- Mary Baker Eddy, "Science and Health" http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (FreeBSD) iD8DBQE/T3/rGb7uCXufRwARAgo3AKCwRn/MfbDSe6ujgA+giOgLGf9XxQCfUh26 eb7gohuxwWOmGXx8ZLUop4s= =6FtZ -----END PGP SIGNATURE----- |
|
|||
|
Binary search. I'd say I have too much time on my hands, but I don't.
<?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" <matijevich@alliancetechnologies.net> > To: <php-general@lists.php.net> > 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 >> > > |
|
|||
|
I think this program will work under the condition that the fault one is
heavier in weight, but the case is that it may be "lighter" OR "heavier". Ok enough of php interview questions. I need to pass a value to a function in .js file which has been linked in a php file which in turn has been included in another php file. ------------------ //main.php fetch DB values and store it in $param. include"first.php" -------------------> ------------------- //first.php <script language='JavaScript' src='/myjs.js'> </script> -------------------> ------------------- myjs.js function fun(param) { .... process with param } -------------------> How can I achieve this? -murugesan ----- Original Message ----- > Binary search. I'd say I have too much time on my hands, but I don't. > > <?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))/////////// First time > $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))///////////Second time > $quarter_eggs = $eggs1_3; > else > $quarter_eggs = $eggs4_6; > > if ($quarter_eggs[0] == $quarter_eggs[1])///////////Third time > $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. |