A brute-force solver for problems about truth tellers and liars.
Below the # Your code goes below!
comment, you should describe the problem. You can check if two people act the same (that is, if they are both truth tellers or both liars) using the equality operator ==
, or if they are different using the inequality operator !=
. You can check if they are a truth teller using the .is_truth_teller()
method, and if they are a liar using the .is_liar()
method.
If you want to quote what somebody has said you can use the person.said()
method.
All the people involved are supplied in an array.
Let's say you have one person, A, that says I'm a liar!
, and you want to check if this is actually possible in a world where people either always lie, or always tell the truth.
First, you need to describe the problem. We need to use the .said()
method to quote A, and we need to describe what A said using .is_liar()
.
The first argument is the amount of people we want. In this case, we only want one person, A.
def verify_conditions_satisfied(people):
A = people[0]
return A.said(A.is_liar())
number_of_people = 1
print(list_solutions(number_of_people, verify_conditions_satisfied))
That will output
[]
because it is actually impossible to say you are a liar:
- If you are a liar, you would be lying about being a liar, hence you are a truth teller. (Contradiction)
- If you are a truth-teller, you would be telling the truth about being a liar, hence you are a liar. (Contradiction)
Here is a more succint way of doing the same thing:
print(list_solutions(1, lambda people: people[0].said(people[0].is_liar())))
Let's call the speaker A, and the brother B. We will want to use .said()
to quote what A said, and then we will have to explain what A did say.
def verify_conditions_satisfied(people):
A, B = people[0], people[1]
return A.said(A.is_liar() or B.is_liar())
number_of_people = 2
print(list_solutions(number_of_people, verify_conditions_satisfied))
This will output
['10']
Each element in the array refers to one combination which works. In this case, there is only one such combination: "A is a truth-teller, B is a liar".
# | A | B |
---|---|---|
1 | T | L |
Here is, again, a shorter (but perhaps less readable) way of writing out the same thing:
print(list_solutions(2, lambda p: p[0].said(p[0].is_liar() or p[1].is_liar())))
In this case, you can use the equality operator:
def verify_conditions_satisfied(people):
A, B = people[0], people[1]
return A.said(A == B)
print(list_solutions(2, verify_conditions_satisfied))
or, less readably:
print(list_solutions(2, lambda p: p[0].said(p[0] == p[1])))
will return:
['01', '11']
This means that A (the person speaking) could be either a truth teller or a liar, because the first digit is 0
in one case, but 1
in the other; but the sister (B) must be a truth-teller, because the second digit in each element is always 1
:
# | A | B |
---|---|---|
1 | L | T |
2 | T | T |