MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply TodayThis is a Matlab adaptation of the Knights and Knaves logical puzzles.

You are in an island where all inhabitants are either *Knights*, who always tell the truth, or *Knaves*, who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same.

Upon arriving to this island you encounter two inhabitants standing at a fork in the road. One of them is a Knight, and the other is a Knave, but you do not know which. One of the roads will lead you to the castle, where you wish to go. Your job is to ask one or several yes/no questions that will allow you to know which one is the correct road.

**Implementation**

You function will take two function handles as inputs (one per inhabitant), and must return a single scalar indicating the correct road (1 for the first road or 2 for the second road):

function correct_road = solver(fA, fB)

You may ask one inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated return a scalar logical value (yes/no questions, where *true* is a yes, and *false* is a no). Strings may refer to the following variables:

- A: true if the first inhabitant is a Knight, false if he is a Knave
- B: true if the second inhabitant is a Knight, false if he is a Knave
- X: 1 if the first road leads to the castle, 2 if the second road leads to the castle

**Asking questions, examples**:

x=fA('A==true');

asks the first inhabitant whether he is a Knight (note: this returns always *true*, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)

x=fB('X==1');

asks the second inhabitant whether the first road leads to the castle (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)

Next problem in this series: Knights and Knaves (part 2)

25 correct solutions
38 incorrect solutions

Last solution submitted on Apr 03, 2016

1 player likes this solution

1 Comment

Jean-Marie Sainthillier
on 3 Apr 2016

I have the feeling it's a cheat no ? We don't stay in the problem's definitions.

1 player likes this solution

1 Comment

Alfonso Nieto-Castanon
on 8 Jan 2013

textbook solution, nice!

2 Comments