## 2018年11月15日木曜日

### Algorithm - Python - 心を読む(準備をしてから)(カードの順序、数とスート、情報)

コード(Emacs)

Python 3

```#!/usr/bin/env python3
''' 問題3 '''
deck = [f'{num}_{suit}'
for suit in ['C', 'D', 'H', 'S']
for num in ['A'] + list(range(2, 11)) + ['J', 'Q', 'K']]

print('deck')
for i, card in enumerate(deck):
print(card, end=' ')
if i % 4 == 3:
print()

def output_first_card(numbers, one_two, cards):
one_index, two_index = one_two[:2]
encode = (numbers[one_index] - numbers[two_index]) % 13
if 0 < encode <= 6:
hidden_index = one_index
other_index = two_index
else:
hidden_index = two_index
other_index = one_index
encode = (numbers[two_index] - numbers[one_index]) % 13
print(f'First card is: {cards[other_index]}')
return hidden_index, other_index, encode

def output_next_three_cards(encode, indices):
if encode == 1:
s, t, f = indices
elif encode == 2:
s, f, t = indices
elif encode == 3:
t, s, f = indices
elif encode == 4:
t, f, s = indices
elif encode == 5:
f, s, t = indices
else:
f, t, s = indices
print(f'Second card is: {deck[s]}')
print(f'Third card is: {deck[t]}')
print(f'Fourth is: {deck[f]}')

def computer_assistant(number):
print('Cards are character strings as shown below.')
print(f'Ordering is: {deck}')
cards, card_indices, card_suits, card_numbers = [[] for _ in range(4)]
num_suits = [0 for _ in range(4)]
i = 0
while len(cards) < 5:
number = number * (i + 1) // (i + 2)
n = number % 52
card_new = deck[n]
if card_new not in cards:
cards.append(card_new)
card_indices.append(n)
card_suits.append(n // 13)
card_numbers.append(n % 13)
num_suits[n // 13] += 1
if num_suits[n // 13] > 1:
pair_suit = n // 13
i += 1
cardh = []
for i in range(5):
if card_suits[i] == pair_suit:
cardh.append(i)
hidden_index, other_index, encode = output_first_card(
card_numbers, cardh, cards)
rem_indices = []
for i in range(5):
if i != hidden_index and i != other_index:
rem_indices.append(card_indices[i])
rem_indices.sort()
output_next_three_cards(encode, rem_indices)
guess = input('What is the hidden card? ')
if guess == cards[hidden_index]:
print('You are a Mind Reader Extraordinaire!')
else:
print('Sorry, not impressed!')
print(f'(hidden: {cards[hidden_index]})')

if __name__ == '__main__':
while True:
s = input('Please give random number of at least 6 digits: ')
if s == 'q':
break
try:
number = int(s)
if number > 99999:
computer_assistant(number)
except Exception as err:
print(err)
```

```\$ ./sample3.py
deck
A_C 2_C 3_C 4_C
5_C 6_C 7_C 8_C
9_C 10_C J_C Q_C
K_C A_D 2_D 3_D
4_D 5_D 6_D 7_D
8_D 9_D 10_D J_D
Q_D K_D A_H 2_H
3_H 4_H 5_H 6_H
7_H 8_H 9_H 10_H
J_H Q_H K_H A_S
2_S 3_S 4_S 5_S
6_S 7_S 8_S 9_S
10_S J_S Q_S K_S
Please give random number of at least 6 digits: 888888
Cards are character strings as shown below.
Ordering is: ['A_C', '2_C', '3_C', '4_C', '5_C', '6_C', '7_C', '8_C', '9_C', '10_C', 'J_C', 'Q_C', 'K_C', 'A_D', '2_D', '3_D', '4_D', '5_D', '6_D', '7_D', '8_D', '9_D', '10_D', 'J_D', 'Q_D', 'K_D', 'A_H', '2_H', '3_H', '4_H', '5_H', '6_H', '7_H', '8_H', '9_H', '10_H', 'J_H', 'Q_H', 'K_H', 'A_S', '2_S', '3_S', '4_S', '5_S', '6_S', '7_S', '8_S', '9_S', '10_S', 'J_S', 'Q_S', 'K_S']
First card is: K_H
Second card is: A_C
Third card is: 3_S
Fourth is: K_S
What is the hidden card? A_H
You are a Mind Reader Extraordinaire!
(hidden: A_H)
Please give random number of at least 6 digits: 100000
Cards are character strings as shown below.
Ordering is: ['A_C', '2_C', '3_C', '4_C', '5_C', '6_C', '7_C', '8_C', '9_C', '10_C', 'J_C', 'Q_C', 'K_C', 'A_D', '2_D', '3_D', '4_D', '5_D', '6_D', '7_D', '8_D', '9_D', '10_D', 'J_D', 'Q_D', 'K_D', 'A_H', '2_H', '3_H', '4_H', '5_H', '6_H', '7_H', '8_H', '9_H', '10_H', 'J_H', 'Q_H', 'K_H', 'A_S', '2_S', '3_S', '4_S', '5_S', '6_S', '7_S', '8_S', '9_S', '10_S', 'J_S', 'Q_S', 'K_S']
First card is: 3_H
Second card is: K_D
Third card is: 2_C
Fourth is: A_S
What is the hidden card? 6_H
You are a Mind Reader Extraordinaire!
(hidden: 6_H)
Please give random number of at least 6 digits: 111111
Cards are character strings as shown below.
Ordering is: ['A_C', '2_C', '3_C', '4_C', '5_C', '6_C', '7_C', '8_C', '9_C', '10_C', 'J_C', 'Q_C', 'K_C', 'A_D', '2_D', '3_D', '4_D', '5_D', '6_D', '7_D', '8_D', '9_D', '10_D', 'J_D', 'Q_D', 'K_D', 'A_H', '2_H', '3_H', '4_H', '5_H', '6_H', '7_H', '8_H', '9_H', '10_H', 'J_H', 'Q_H', 'K_H', 'A_S', '2_S', '3_S', '4_S', '5_S', '6_S', '7_S', '8_S', '9_S', '10_S', 'J_S', 'Q_S', 'K_S']
First card is: 10_C
Second card is: 5_D
Third card is: 6_C
Fourth is: 7_D
What is the hidden card? K_C
You are a Mind Reader Extraordinaire!
(hidden: K_C)
Please give random number of at least 6 digits: 99999
Please give random number of at least 6 digits: a
invalid literal for int() with base 10: 'a'
Please give random number of at least 6 digits: q
\$
```