2018年11月16日金曜日

開発環境

問題解決のPythonプログラミング ―数学パズルで鍛えるアルゴリズム的思考 (Srini Devadas (著)、黒川 利明 (翻訳)、オライリージャパン)の3章(心を読む(準備をしてから))、練習問題(パズル問題4)を取り組んでみる。

コード(Emacs)

Python 3

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

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


def computer_assistant_four_cards(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) < 4:
        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 % 4)
            card_numbers.append(n // 4)
            num_suits[n % 4] += 1
        i += 1
    for i in range(4):
        for j in range(4):
            if i == j:
                break
            t = (card_indices[i] - card_indices[j]) % 52
            if 1 <= t <= 13:
                encode = t
                hidden_card = cards.pop(i)
                if i < j:
                    first_card = cards.pop(j - 1)
                else:
                    first_card = cards.pop(j)
                second_card, third_card = cards
                break
        if len(cards) == 2:
            break

    if encode <= 8:
        print('(front)')
        if encode == 1:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(left)')
        elif encode == 2:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(right)')
        elif encode == 3:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(left)')
        elif encode == 4:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(right)')
        elif encode == 5:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(left)')
        elif encode == 6:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(right)')
        elif encode == 7:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(left)')
        else:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(right)')
    else:
        print('(back)')
        if encode == 8:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(left)')
        elif encode == 9:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(right)')
        elif encode == 10:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(left)')
        elif encode == 11:
            print(f'First card is: {first_card}(left)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(right)')
        elif encode == 12:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(left)')
            print(f'Third card is: {third_card}(left)')
        else:
            print(f'First card is: {first_card}(right)')
            print(f'Second card is: {second_card}(right)')
            print(f'Third card is: {third_card}(left)')

    guess = input('What is the hidden card? ')
    if guess == hidden_card:
        print('You are a Mind Reader Extraordinaire!')
    else:
        print('Sorry, not impressed!')
    print(f'(hidden: {hidden_card})')


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_four_cards(number)
        except Exception as err:
            print(err)

入出力結果(Terminal, Jupyter(IPython))

$ ./sample4.py
deck
A_C A_D A_H A_S 
2_C 2_D 2_H 2_S 
3_C 3_D 3_H 3_S 
4_C 4_D 4_H 4_S 
5_C 5_D 5_H 5_S 
6_C 6_D 6_H 6_S 
7_C 7_D 7_H 7_S 
8_C 8_D 8_H 8_S 
9_C 9_D 9_H 9_S 
10_C 10_D 10_H 10_S 
J_C J_D J_H J_S 
Q_C Q_D Q_H Q_S 
K_C K_D K_H K_S 
Please give random number of at least 6 digits: 100000
Cards are character strings as shown below.
Ordering is: ['A_C', 'A_D', 'A_H', 'A_S', '2_C', '2_D', '2_H', '2_S', '3_C', '3_D', '3_H', '3_S', '4_C', '4_D', '4_H', '4_S', '5_C', '5_D', '5_H', '5_S', '6_C', '6_D', '6_H', '6_S', '7_C', '7_D', '7_H', '7_S', '8_C', '8_D', '8_H', '8_S', '9_C', '9_D', '9_H', '9_S', '10_C', '10_D', '10_H', '10_S', 'J_C', 'J_D', 'J_H', 'J_S', 'Q_C', 'Q_D', 'Q_H', 'Q_S', 'K_C', 'K_D', 'K_H', 'K_S']
(back)
First card is: 8_C(left)
Second card is: A_D(right)
Third card is: 8_S(right)
What is the hidden card? 10_S
You are a Mind Reader Extraordinaire!
(hidden: 10_S)
Please give random number of at least 6 digits: 888888
Cards are character strings as shown below.
Ordering is: ['A_C', 'A_D', 'A_H', 'A_S', '2_C', '2_D', '2_H', '2_S', '3_C', '3_D', '3_H', '3_S', '4_C', '4_D', '4_H', '4_S', '5_C', '5_D', '5_H', '5_S', '6_C', '6_D', '6_H', '6_S', '7_C', '7_D', '7_H', '7_S', '8_C', '8_D', '8_H', '8_S', '9_C', '9_D', '9_H', '9_S', '10_C', '10_D', '10_H', '10_S', 'J_C', 'J_D', 'J_H', 'J_S', 'Q_C', 'Q_D', 'Q_H', 'Q_S', 'K_C', 'K_D', 'K_H', 'K_S']
(back)
First card is: J_D(left)
Second card is: A_C(right)
Third card is: 7_H(left)
What is the hidden card? K_S
You are a Mind Reader Extraordinaire!
(hidden: K_S)
Please give random number of at least 6 digits: 111111
Cards are character strings as shown below.
Ordering is: ['A_C', 'A_D', 'A_H', 'A_S', '2_C', '2_D', '2_H', '2_S', '3_C', '3_D', '3_H', '3_S', '4_C', '4_D', '4_H', '4_S', '5_C', '5_D', '5_H', '5_S', '6_C', '6_D', '6_H', '6_S', '7_C', '7_D', '7_H', '7_S', '8_C', '8_D', '8_H', '8_S', '9_C', '9_D', '9_H', '9_S', '10_C', '10_D', '10_H', '10_S', 'J_C', 'J_D', 'J_H', 'J_S', 'Q_C', 'Q_D', 'Q_H', 'Q_S', 'K_C', 'K_D', 'K_H', 'K_S']
(front)
First card is: 4_C(right)
Second card is: 5_S(left)
Third card is: 3_D(left)
What is the hidden card? 5_D
You are a Mind Reader Extraordinaire!
(hidden: 5_D)
Please give random number of at least 6 digits: 1111111
Cards are character strings as shown below.
Ordering is: ['A_C', 'A_D', 'A_H', 'A_S', '2_C', '2_D', '2_H', '2_S', '3_C', '3_D', '3_H', '3_S', '4_C', '4_D', '4_H', '4_S', '5_C', '5_D', '5_H', '5_S', '6_C', '6_D', '6_H', '6_S', '7_C', '7_D', '7_H', '7_S', '8_C', '8_D', '8_H', '8_S', '9_C', '9_D', '9_H', '9_S', '10_C', '10_D', '10_H', '10_S', 'J_C', 'J_D', 'J_H', 'J_S', 'Q_C', 'Q_D', 'Q_H', 'Q_S', 'K_C', 'K_D', 'K_H', 'K_S']
(front)
First card is: 10_S(right)
Second card is: 7_H(left)
Third card is: 7_D(right)
What is the hidden card? Q_D
You are a Mind Reader Extraordinaire!
(hidden: Q_D)
Please give random number of at least 6 digits: 1
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
$

0 コメント:

コメントを投稿

関連コンテンツ