Problems of liars and honesty

Introduction

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.

How to proceed

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.

Judgment of liar or honesty

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
A combination of liar and honesty

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
Combination of cards to deal

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.

Actual code

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()
Execution result

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   
in conclusion

It was a good problem for brain teasers.

Recommended Posts

Problems of liars and honesty
Problems of liars and honesty
About problems and solutions of OpenPyXL (Ver 3.0 version)
[Competition Pro] Summary of stock buying and selling problems
FizzBuzz problems here and there
Mechanism of pyenv and virtualenv
Pre-processing and post-processing of pytest
Combination of recursion and generator
Combination of anyenv and direnv
Explanation and implementation of SocialFoceModel
Differentiation of sort and generalization of sort
Coexistence of pyenv and autojump
Use and integration of "Shodan"
Comparison of Hungarian law and general-purpose solver for allocation problems
[Tips] Problems and solutions in the development of python + kivy
Occurrence and resolution of tensorflow.python.framework.errors_impl.FailedPreconditionError
Comparison of Apex and Lamvery
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
Environment construction of python and opencv
Various of Tweepy. Ma ♡ and ♡ me ♡
Combinatorial optimization-typical problems and execution methods
Order of arguments of RegularGridInterpolator and interp2d
The story of Python and the story of NaN
Explanation and implementation of PRML Chapter 4
Introduction and Implementation of JoCoR-Loss (CVPR2020)
Benefits and examples of using RabbitMq
Danger of mixing! ndarray and matrix
Installation of SciPy and matplotlib (Python)
Significance of machine learning and mini-batch learning
Introduction and implementation of activation function
Memorandum of saving and loading model
Misunderstandings and interpretations of Luigi's dependencies
Explanation and implementation of simple perceptron
Calculation of homebrew class and existing class
This and that of python properties
Design of experiments and combinatorial optimization
Installation and easy usage of pytest
Clash of Clans and image analysis (3)
Features of symbolic and hard links
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Aggregation and visualization of accumulated numbers
Reputation of Python books and reference books