Python: I tried a liar and an honest tribe

Problems of Liars and Honesty I don't know what number it is, but it looks interesting so I tried it.

python


import itertools

#Return consistent answers from the player and card deck
consistents = (
  lambda players: lambda card_deck: lambda statements:
  (
    (hands, is_each_honest) 
    for hands in handss(card_deck) 
    for is_each_honest in is_each_honests
    if  statements(hands) == is_each_honest 
  )
)
#Generator that returns a tuple of each card's hand
handss = lambda card_deck: itertools.permutations(card_deck)

#Tuples of tuples whether each player is honest(Decisive 2)
is_each_honests = (
  (True, False, False, True)
  ,(False, True, True, False)
)

#data
players = (0, 1, 2, 3)
card_deck = (1, 2, 3, 4)

statements = lambda hands: (
  hands[0] % 2 == 0
  , hands[1] in (3, 4)
  , hands[1] in (3, 4)    # (Decisive 1)
  , hands[3] == 1
)

#Function application and display
for e in consistents(players)(card_deck)(statements):
  print(e)

python


#result:
((1, 3, 2, 4), (False, True, True, False))
((1, 3, 4, 2), (False, True, True, False))
((1, 4, 2, 3), (False, True, True, False))
((1, 4, 3, 2), (False, True, True, False))
((3, 4, 1, 2), (False, True, True, False))
((4, 2, 3, 1), (True, False, False, True))

There is a definite decision.

Decisive 1: "B is honest" => "B is correct" => B is saying => Converted to "B's card is 3 or 4"

Once the card is handed, the truth of each statement is decided, which simplifies the problem.

Decisive 2: Liar / Honesty two people each => B and C are the same statement => B and C are the same family => A and D are the opposite family

There are a total of 6 ways if there are no restrictions, but when B and C are in the same family, the variation is reduced to 2 ways.

python


>>> players = 0, 1, 2, 3
#No restrictions
>>> tuple( tuple( not e in liars for e in players ) for liars in itertools.combinations(players, 2))
((False, False, True, True), (False, True, False, True), (False, True, True, False), (True, False, False, True), (True, False, True, False), (True, True, False, False))    #6 ways
#Limited Liar is AD or otherwise BC
>>> tuple( tuple( not e in liars for e in players ) for liars in itertools.combinations(players, 2) if liars in ((0,3),(1,2)))
((False, True, True, False), (True, False, False, True))    #2 ways

You can calculate it, but there are only two, so I wonder if it's okay to write it directly.

Based on the above

A little short

I will try.

Decisive 1 => Combine B and C

B and C are the same whether they are honest or not.

Set 2 => True / False

If there are only two ways, it can be expressed by a simple logical value instead of a tuple. If the display is important, you can devise it a little.

python


import itertools

#Return consistent answers from the player and card deck
consistents = (
  lambda players: lambda card_deck: lambda statements:
  (
    (hands, is_AD_honest) 
    for hands in itertools.permutations(card_deck)
    for is_AD_honest in (True, False)    # (Decisive 2)
    if  statements(hands) == (is_AD_honest, not is_AD_honest, is_AD_honest) # (Decisive 1) 
  )
)

#data
players = (0, 1, 2, 3)
card_deck = (1, 2, 3, 4)

statements = lambda hands: (
  hands[0] % 2 == 0
  , hands[1] in (3, 4)    # (Decisive 1)
  , hands[3] == 1
)

#Function application and display
for hands, is_AD_honest in consistents(players)(card_deck)(statements):
  print(
    (hands, (is_AD_honest, not is_AD_honest, not is_AD_honest, is_AD_honest)) # (Decisive 2)
  )

It's easy to understand, but it's a little shorter.

But in the first place ...

Isn't it too hard?

If each statement is different (for example, if C says "B is a liar"), the categorical part becomes useless. I will start over.

This is what seems to work even if you rewrite the contents of each statement statements.

python


import itertools

#Return consistent answers from the player and card deck
consistents = (
  lambda players: lambda card_deck: lambda statements:
  (
    (hands, is_each_honest) 
    for hands in handss(card_deck) 
    for is_each_honest in is_each_honests(players)
    if  statements(hands) == is_each_honest 
  )
)
#Generator that returns a tuple of each card's hand
handss = lambda card_deck: itertools.permutations(card_deck)

#A generator that returns a tuple of whether each player is honest
is_each_honests = lambda players:( 
  tuple( e in honests for e in players ) 
  for honests in itertools.combinations(players, 2)
)

#data
players = (0, 1, 2, 3)
card_deck = (1, 2, 3, 4)

statements = lambda hands: (
  hands[0] % 2 == 0
  , hands[1] in (3, 4)
  , hands[1] in (3, 4)    
  , hands[3] == 1
)

#Function application and display
for e in consistents(players)(card_deck)(statements):
  print(e)

However, it is still valid to "convert a reference to a person into a claim about a card's hand," as we did in Decisive 1.

Recommended Posts

Python: I tried a liar and an honest tribe
I created a class in Python and tried duck typing
I tried sending an email with python.
I tried to make a periodical process with Selenium and Python
I tried a functional language with Python
I thought a Python class variable was an instance variable and died
I tried to create a sample to access Salesforce using Python and Bottle
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried playing a typing game in Python
I wrote a class in Python3 and Java
[Memo] I tried a pivot table in Python
I tried function synthesis and curry with python
I tried reading a CSV file using Python
I tried adding a Python3 module in C
I tried sending an email with SendGrid + Python
I tried Python> autopep8
I tried Python> decorator
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I tried pipenv and asdf for Python version control
I tried running python etc. from a bat file
I tried to implement a pseudo pachislot in Python
I tried to implement an artificial perceptron with python
I made a LINE BOT with Python and Heroku
I tried to automatically generate a password with Python3
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried using Python (3) instead of a scientific calculator
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I tried fp-growth with python
I tried scraping with Python
I compared Java and Python!
I tried Python C extension
I made a python text
I tried scraping with python
I tried [scraping] fashion images and text sentences in Python.
I tried to implement a one-dimensional cellular automaton in Python
I tried sending an email from Amazon SES with Python
I got an error in vim and zsh in Python 3.7 series
I tried follow management with Twitter API and Python (easy)
[Markov chain] I tried to read a quote into Python.
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
I tried crawling and scraping a horse racing site Part 2
I tried to enumerate the differences between java and python
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried a stochastic simulation of a bingo game with Python
A python beginner tried to intern at an IT company
I made a Chatbot using LINE Messaging API and Python
I made a server with Python socket and ssl and tried to access it from a browser
I also tried to imitate the function monad and State monad with a generator in Python
[5th] I tried to make a certain authenticator-like tool with python
Python a + = b and a = a + b are different
I made a simple circuit with Python (AND, OR, NOR, etc.)
[Python3] I made a decorator that declares undefined functions and methods.
I tried to convert a Python file to EXE (Recursion error supported)
I tried to touch Python (installation)
[2nd] I tried to make a certain authenticator-like tool with python
Pydroid 3 --I tried OpenCV and TensorFlow options for IDE for Python 3 (Android)