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

Learn moreOpportunities for recent engineering grads.

Apply Today*This is a Matlab adaptation of the* Knives 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*.

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

**Chapter 3**

That mob of islanders turned out to be a friendly bunch after all, and with their help you reach the castle in no time. The doors to the castle are guarded by a sentinel who will only let you in if you answer correctly the day's password. Fortunately islanders are not particularly security conscious and the guard is happy to answer as many questions as you see fit. What is today's password?

**Details**

You are given a function handle **QUESTION** that allows you to query the guard (either a Knight or a Knave, you do not know which). The function y=QUESTION(str) takes a string *str* as input (the 'question'), and returns a logical value (the yes/no 'answer' the guard would give to this question). Valid questions are any valid matlab syntax. The guard has access to the following variables (his pool of 'knowledge'):

**KNIGHT**: Islander's type (true for a Knight, false for a Knave)**PASSWORD**: Today's password (a char array of all-capital letters)

You may query the guard by evaluating the function **QUESTION**. For example x=QUESTION('KNIGHT==true') asks the guard whether he is a Knight.

Your function should return the correct password.

10 correct solutions
12 incorrect solutions

Last solution submitted on Aug 04, 2014

1 player likes this solution

1 Comment

Alfonso Nieto-Castanon
on 17 Feb 2013

Nice way to minimize number of queries!

2 Comments