Truth-Tellers and Liars: Deduce Two Island Identities
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Tests systematic logical deduction by resolving a two-person truth-teller/liar puzzle through exhaustive case analysis.
Background
On a remote island, every inhabitant belongs to exactly one of two groups: Knights, who always tell the truth, and Knaves, who always lie. You meet two islanders, Perlica and Chen.
Perlica says: "At least one of us is a Knave." Chen says: "Perlica is a Knave."
Your task is to determine the group of each speaker — Knight or Knave — given that both statements must be consistent with the rules.
Task
Write a function that returns a dictionary with three keys:
"Perlica"— either"Knight"or"Knave""Chen"— either"Knight"or"Knave""reasoning"— a string that explains the derivation: start from the assumption that Perlica is a Knave, show why it leads to a contradiction, conclude that Perlica must be a Knight, then use Perlica's true statement to determine Chen's identity.
The "reasoning" value must match the expected output exactly.
Rules
- A Knight's statement is always true.
- A Knave's statement is always false.
- Every islander is either a Knight or a Knave — no other type exists.
Constraints
- The function takes no arguments.
- The return value is a
dictwith exactly the three string keys described above. - There is exactly one consistent assignment; return that unique solution.
- The
"reasoning"string must follow the derivation order specified in the task description.
Examples
Example 1
Input: resolve_two_knights_and_knaves()
Output: {'Perlica': 'Knight', 'Chen': 'Knave', 'reasoning': 'If Perlica were a knave, her statement would be false, meaning neither person is a knave, which is a contradiction. Therefore Perlica is a knight. Her statement is true, so at least one person is a knave. Chen says Perlica is a knave, which is false, so Chen is a knave.'}
Hints
Hint 1
Try assuming Perlica is a Knave and follow the consequences of her statement being false — see if you reach a contradiction.
Hint 2
Once you fix Perlica's identity, evaluate whether Chen's claim about Perlica is true or false, then match that truth value to Chen's group.
Hint 3
Notice that the phrase 'at least one of us' includes the speaker herself — this self-reference is what makes the Knave assumption collapse immediately.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core rule is that a speaker's group must match the truth value of their statement: a Knight says something true, a Knave says something false. A valid solution is an assignment of groups where every speaker satisfies this rule simultaneously.
Step 1 — Try assuming Perlica is a Knave. If Perlica is a Knave, her statement ("at least one of us is a Knave") must be false. Its negation is "neither of us is a Knave." But we just assumed Perlica is a Knave — an immediate contradiction. So this case is impossible.
Step 2 — Conclude Perlica is a Knight. Since the only alternative fails, Perlica must be a Knight. Her statement is therefore true: at least one of the two is a Knave. Perlica herself is a Knight, so the Knave must be Chen.
Step 3 — Verify Chen. Chen claims "Perlica is a Knave." Perlica is a Knight, so this claim is false. A false statement coming from Chen is consistent with Chen being a Knave. The assignment checks out.
Why it is unique. There are only four possible group assignments for two people, and the other three all produce contradictions — most directly via Perlica's self-referential statement, which cannot truthfully be uttered by a Knave.
Complexity. The function returns a hard-coded constant derived from a finite case analysis; it runs in O(1) time and O(1) space.
from typing import Dict
def resolve_two_knights_and_knaves() -> Dict[str, str]:
# If Perlica were a Knave, her statement would be false, meaning
# neither person is a Knave — but that contradicts Perlica being one.
# So Perlica must be a Knight. Her statement is therefore true, meaning
# at least one person is a Knave. Since Perlica is a Knight,
# that Knave must be Chen. Chen's claim that Perlica is a Knave
# is false, which is exactly what a Knave would say. Consistent.
return {
"Perlica": "Knight",
"Chen": "Knave",
"reasoning": "If Perlica were a knave, her statement would be false, meaning neither person is a knave, which is a contradiction. Therefore Perlica is a knight. Her statement is true, so at least one person is a knave. Chen says Perlica is a knave, which is false, so Chen is a knave."
}