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?


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

35.0% Correct | 65.0% Incorrect
Last Solution submitted on Apr 14, 2023

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers32

Suggested Problems

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!