開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の5章(集合と確率を操作する)、5.4(プログラミングチャレンジ)、問題5-4(トランプをよく切る)を取り組んでみる。
問題5-4(トランプをよく切る)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
if __name__ == '__main__':
cards = [
Card(suit, rank)
for suit in ['clubs', 'spades', 'hurts', 'diamonds']
for rank in (
['ace'] + list(range(2, 11)) + ['jack', 'queen', 'king']
)
]
for _ in range(2):
random.shuffle(cards)
print('--------------------------------------------------')
print('\n'.join(map(str, cards)))
入出力結果(Terminal, IPython)
$ ./sample4.py -------------------------------------------------- 3 of clubs ace of clubs 7 of spades ace of diamonds queen of clubs 7 of diamonds ace of hurts 10 of diamonds king of clubs ace of spades 2 of spades king of spades 10 of hurts 6 of clubs 8 of spades 9 of clubs 2 of clubs 6 of diamonds queen of hurts jack of diamonds 10 of spades 5 of diamonds 7 of clubs 4 of spades king of diamonds jack of hurts 5 of clubs 3 of hurts king of hurts jack of clubs 5 of hurts 9 of diamonds jack of spades 8 of clubs 6 of hurts 4 of clubs 9 of hurts 8 of hurts 4 of hurts 2 of diamonds queen of diamonds 7 of hurts 9 of spades 2 of hurts 3 of spades 8 of diamonds 6 of spades queen of spades 3 of diamonds 10 of clubs 5 of spades 4 of diamonds -------------------------------------------------- 10 of clubs 3 of hurts 6 of diamonds 7 of diamonds 8 of spades 2 of clubs 10 of diamonds 8 of diamonds 9 of diamonds ace of spades 4 of hurts 6 of clubs 3 of spades 5 of diamonds 4 of spades queen of spades 6 of hurts king of hurts jack of clubs 8 of clubs 2 of spades queen of hurts queen of diamonds jack of hurts king of clubs king of diamonds king of spades 9 of clubs 5 of spades 10 of hurts 3 of diamonds 5 of hurts 7 of spades queen of clubs 9 of spades 7 of hurts 5 of clubs 7 of clubs jack of spades jack of diamonds ace of diamonds 8 of hurts 6 of spades 2 of diamonds 4 of diamonds 10 of spades 4 of clubs 9 of hurts 3 of clubs ace of clubs ace of hurts 2 of hurts $
0 コメント:
コメントを投稿