[PYTHON] Problems of liars and honesty

I tried to solve the liar and honest problem with Python. I haven't matched the answers. Python easy.

import itertools

#If you are a liar, answer the opposite
def ans( person, liars, b):
    if person in liars:
        b = not b
    return b

def print_res(liars, cards):
    print('-----')
    for i in range(4):
        print( '{}:{} {}'.format('ABCD'[i], 'Lie' if i in liars else 'Positive', cards[i]))

#A combination of a liar and a card
liars_g = itertools.combinations(list(range(4)),2) #Liar person
cards_g = itertools.permutations(list(range(1,5))) #card
for liars, cards in itertools.product(liars_g,cards_g):
    #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.
    if  ans( 0, liars, cards[0] % 2 == 0) and \
        ans( 1, liars, cards[1] in (3,4)) and \
        ans( 2, liars, 1 not in liars) and \
        ans( 3, liars, cards[3] == 1):
        print_res(liars, cards)

answer

-----
A:Lie 1
B:Positive 3
C:Positive 2
D:Lie 4
-----
A:Lie 1
B:Positive 3
C:Positive 4
D:Lie 2
-----
A:Lie 1
B:Positive 4
C:Positive 2
D:Lie 3
-----
A:Lie 1
B:Positive 4
C:Positive 3
D:Lie 2
-----
A:Lie 3
B:Positive 4
C:Positive 1
D:Lie 2
-----
A:Positive 4
B:Lie 2
C:Lie 3
D:Positive 1

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 ♡
Basic knowledge of Linux and basic commands
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
Explanation and implementation of ESIM algorithm
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