2018年11月14日水曜日

開発環境

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

コード(Emacs)

Python 3

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


def output_first_card(numbers, one, two, cards):
    one_number, two_number = numbers[one], numbers[two]
    if one_number < two_number:
        hidden, other = one, two
        encode = two_number - one_number
    else:
        hidden, other = two, one
        encode = one_number - two_number
    if encode <= 6:
        dir = 'left'
    else:
        dir = 'right'
        encode -= 6
    print(f'First card is: {cards[other]} (from {dir})')
    return hidden, other, encode


def output_next_three_cards(code, ind):
    if code == 1:
        second, third, fourth = ind[0], ind[1], ind[2]
    elif code == 2:
        second, third, fourth = ind[0], ind[2], ind[1]
    elif code == 3:
        second, third, fourth = ind[1], ind[0], ind[2]
    elif code == 4:
        second, third, fourth = ind[1], ind[2], ind[0]
    elif code == 5:
        second, third, fourth = ind[2], ind[0], ind[1]
    else:
        second, third, fourth = ind[2], ind[1], ind[0]

    print('Second card is:', deck[second])
    print('Third card is:', deck[third])
    print('Fourth card is:', deck[fourth])


def computer_assistant(number):
    print('Cards are character strings as show below.')
    print(f'Ordering is: {deck}')
    cards, cind, cardsuits, cnumbers = [], [], [], []
    numsuits = [0, 0, 0, 0]
    cards = []
    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)
            cind.append(n)
            cardsuits.append(n % 4)
            cnumbers.append(n // 4)
            numsuits[n % 4] += 1
            if numsuits[n % 4] > 1:
                pairsuit = n % 4
        i += 1
    cardh = []
    for i in range(5):
        if cardsuits[i] == pairsuit:
            cardh.append(i)
    hidden, other, encode = output_first_card(cnumbers, *cardh[:2], cards)
    remindices = [cind[i] for i in range(5)
                  if i != hidden and i != other]
    remindices.sort()
    output_next_three_cards(encode, remindices)
    guess = input('What is the hidden card? ')
    if guess == cards[hidden]:
        print('You are a Mind Reader Extraordinaire!')
    else:
        print('Sorry not impressed!')
    print(f'(hidden card: {cards[hidden]})')


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

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

$ ./sample2.py
Please give random number of at least 6 digits: 100000
Cards are character strings as show 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']
First card is: 7_D (from left)
Second card is: 10_S
Third card is: 8_S
Fourth card is: 8_C
What is the hidden card? A_D
You are a Mind Reader Extraordinaire!
(hidden card: A_D)
Please give random number of at least 6 digits: 111111
Cards are character strings as show 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']
First card is: 5_D (from left)
Second card is: 2_D
Third card is: 5_S
Fourth card is: 4_C
What is the hidden card? 3_D
You are a Mind Reader Extraordinaire!
(hidden card: 3_D)
Please give random number of at least 6 digits: 1111111
Cards are character strings as show 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']
First card is: Q_D (from left)
Second card is: 10_S
Third card is: 4_C
Fourth card is: 7_H
What is the hidden card? 7_D
You are a Mind Reader Extraordinaire!
(hidden card: 7_D)
Please give random number of at least 6 digits: 11111111
Cards are character strings as show 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']
First card is: J_D (from right)
Second card is: 7_H
Third card is: 8_S
Fourth card is: A_S
What is the hidden card? A_D
You are a Mind Reader Extraordinaire!
(hidden card: A_D)
Please give random number of at least 6 digits: 888888
Cards are character strings as show 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']
First card is: 10_H (from left)
Second card is: J_D
Third card is: A_C
Fourth card is: K_S
What is the hidden card? 7_H
You are a Mind Reader Extraordinaire!
(hidden card: 7_H)
Please give random number of at least 6 digits: q
$

0 コメント:

コメントを投稿