Problem 1189. Knights and Knaves (part 3)
This 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.
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.
Solution Stats
Problem Comments
-
4 Comments
If the guard is a knave, and I ask him what the password is, what response do I get? Do I still get an array of chars? Does he replace 'A' with 'Z', 'B' with 'Y', etc..?
questions can only be statements that evaluate to a scalar logical value (yes/no questions), so you can ask things like QUESTION('PASSWORD(1)==''A'''); or QUESTION('strcmp(PASSWORD,''ASDF'')'); but if you ask a question that returns a vector you will simply get an error.
Very fun problem Thanks!
Thank you for taking the time to make up this series of problems.
Solution Comments
Show commentsProblem Recent Solvers33
Suggested Problems
-
2625 Solvers
-
Project Euler: Problem 5, Smallest multiple
1489 Solvers
-
Piecewise linear interpolation
553 Solvers
-
539 Solvers
-
Remove element(s) from cell array
1728 Solvers
More from this Author38
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!