開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の9章(集合と辞書)、9.5(練習問題)、2.を解いてみる。
9.5(練習問題)、2.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8
def matingPairs(males, females):
result = set()
temp = set(females)
while males:
females = set(temp)
male = males.pop()
while females:
female = females.pop()
result.add((male, female))
return result
males = {1, 'a', 2, 'b', 3}
females = {'c', 4, 5, 'd', 'e'}
print('males: {0}'.format(males))
print('females: {0}'.format(females))
print('mating pairs: {0}'.format(matingPairs(males, females)))
入出力結果(Terminal)
$ ./sample.py
males: {1, 2, 3, 'b', 'a'}
females: {'d', 'e', 4, 5, 'c'}
mating pairs: {(1, 'd'), (3, 'e'), ('b', 'c'), ('a', 4), (1, 'c'), ('a', 'c'), (2, 'd'), ('b', 'd'), (2, 5), (3, 'd'), (1, 5), (2, 'e'), ('b', 'e'), (3, 'c'), ('a', 'e'), ('b', 4), (1, 4), ('a', 'd'), (3, 5), (1, 'e'), ('b', 5), ('a', 5), (2, 'c'), (3, 4), (2, 4)}
$
0 コメント:
コメントを投稿