At some point, I found the following "problem between liar and honest people". I was curious, so I tried programming in Python.
In a village, there were four people, Mr. A, Mr. B, Mr. C, and Mr. D. Two of them are known to be liar, and two of them are known to be honest. Liars always lie, and honests always answer honestly. They are logical and there are no mistakes. You have four cards labeled 1, 2, 3, and 4. No card has the same number. Randomly selected from there, one by one was given to them. They said: Mr. A: My card is an even number. Mr. B: My card is either 3 or 4. Mr. C: Mr. B is an honest family. Mr. D: My card is 1. Create a program that shows the numbers on the cards that may have been dealt to them and who is the liar / honest.
I don't know who A, B, C, or D is a liar or an honest person (naturally because this is a problem), so I'm going to use all the patterns assuming that each person is a liar or an honest person. Determine whether each person is a liar or an honest person. Next, I thought that it would be better to find the solution when the assumed pattern and the judged pattern match, so I will create a program like that.
As shown below, the cards dealt will determine whether Mr. A, Mr. B, Mr. C, and Mr. D are liars or honest. However, Mr. C will judge by Mr. B's liar / honesty.
Mr. A:
Cards dealt | Content of remark (fixed) | Judgment |
---|---|---|
Even | 私のカードは、Evenです. | Honest |
Not even | My card is even. | Liar |
Mr. B:
Cards dealt | Content of remark (fixed) | Judgment |
---|---|---|
3 or 4 | My card is either 3 or 4. | Honest |
Neither 3 nor 4 | My card is either 3 or 4. | Liar |
Mr. C:
Mr. B | Content of remark (fixed) | Judgment |
---|---|---|
Honest | Bさんは、Honest者です. | Honest |
Liar | Mr. B is an honest person. | Liar |
Mr. D:
Cards dealt | Content of remark (fixed) | Judgment |
---|---|---|
1 | My card is 1. | Honest |
Not 1 | My card is 1. | Liar |
The combination of liar and honesty is the following 6 patterns. Liars are 0 and honesty is 1.
Mr. A | Mr. B | Mr. C | Mr. D |
---|---|---|---|
0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 |
0 | 1 | 1 | 0 |
1 | 0 | 0 | 1 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 0 |
The combination of cards to deal is calculated from the permutation n </ sub> P k </ sub> (the number when k are selected from n). This time, use 4 </ sub> P 4 </ sub>. Python's itertools package has a function permutations () that asks for permutations, so use that.
Python
#Problems of liars and honesty
import itertools
#Judgment by Mr. A: My card is an even number.
def judge_A(card_no):
if (card_no % 2) == 0:
ret = 1
else:
ret = 0
return ret
#Mr. B's judgment: My card is either 3 or 4.
def judge_B(card_no):
if card_no == 3 or card_no == 4:
ret = 1
else:
ret = 0
return ret
#Judgment of Mr. C: Mr. B is an honest family.
def judge_C(judge_of_B):
if judge_of_B == 1:
ret = 1
else:
ret = 0
return ret
#Judgment by Mr. D: My card is 1.
def judge_D(card_no):
if card_no == 1:
ret = 1
else:
ret = 0
return ret
#Judgment
# deal_card :Cards dealt (1, 2, 3, 4). A list of 4 elements.
# [0]=Mr. A,[1]=Mr. B, "2"=Mr. C,[3]=Mr. D
# return :judgment result. A list of 4 elements.
# [0]=Mr. A,[1]=Mr. B, "2"=Mr. C,[3]=Mr. D
def judge(deal_card):
lh_judge = [None, None, None, None]
lh_judge[0] = judge_A(deal_card[0])
lh_judge[1] = judge_B(deal_card[1])
lh_judge[2] = judge_C(lh_judge[1])
lh_judge[3] = judge_D(deal_card[3])
return lh_judge
def main():
#A combination of liar and honesty
lh_comb = [[0, 0, 1, 1],
[0, 1, 0, 1],
[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 1, 0, 0]]
#Cards to deal(1 to 4)Combination (permutation: nPk)
cards_comb = list(itertools.permutations([1, 2, 3, 4]))
#Investigate by the combination of liar and honesty x card combination.
print("\
Liar(0)/ Honest(1) |Cards dealt\n\
Mr. A, Mr. B, Mr. C, Mr. D|Mr. A, Mr. B, Mr. C, Mr. D\n\
---------------------------------------------------------")
for lh_assumption in lh_comb:
for deal_card in cards_comb:
lh_judge = judge(deal_card)
if lh_assumption == lh_judge:
print("{:^7d}{:^7d}{:^7d}{:^7d}|{:^7d}{:^7d}{:^7d}{:^7d}".
format(lh_judge[0],lh_judge[1],lh_judge[2],lh_judge[3],
deal_card[0],deal_card[1],deal_card[2],deal_card[3]))
return
if __name__ == '__main__':
main()
It turned out that there are the following 6 cases.
Liar(0)/ Honest(1) |Cards dealt
Mr. A, Mr. B, Mr. C, Mr. D|Mr. A, Mr. B, Mr. C, Mr. D
---------------------------------------------------------
0 1 1 0 | 1 3 2 4
0 1 1 0 | 1 3 4 2
0 1 1 0 | 1 4 2 3
0 1 1 0 | 1 4 3 2
0 1 1 0 | 3 4 1 2
1 0 0 1 | 4 2 3 1
It was a good problem for brain teasers.
Recommended Posts