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