開発環境
- 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(練習問題)、12.を解いてみる。
9.5(練習問題)、12.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
def dbConsistent(d):
ks = list(d.keys())
heading = set(d[ks[0]].keys())
for k in ks[1:]:
if d[k].keys() != heading:
return False
return True
scientists1 = {
'Jグドール': {
'姓': 'ゴドール',
'名': 'ジェーン',
'生年': 1934,
'没年': None,
'注目': '霊長類研究者',
'著作': ['In the Shadow of Man', 'The Chimpanzees of Gombe']
},
'Rフランクリン': {
'姓': 'フランクリン',
'名': 'ロザリンド',
'生年': 1920,
'没年': 1957,
'注目': 'DNAの発見に貢献'
},
'Rカーソン': {
'姓': 'カーソン',
'名': 'レイチェル',
'生年': 1907,
'没年': 1964,
'注目': 'DDTの毒外に注意を喚起',
'著作': ['沈黙の春']
}
}
scientists2 = {
'Jグドール': {
'姓': 'ゴドール',
'名': 'ジェーン',
'生年': 1934,
'没年': None,
'注目': '霊長類研究者',
'著作': ['In the Shadow of Man', 'The Chimpanzees of Gombe']
},
'Rカーソン': {
'姓': 'カーソン',
'名': 'レイチェル',
'生年': 1907,
'没年': 1964,
'注目': 'DDTの毒外に注意を喚起',
'著作': ['沈黙の春']
}
}
for scientists in [scientists1, scientists2]:
print('database: {0}'.format(scientists))
print('database consistent: {0}'.format(dbConsistent(scientists)))
入出力結果(Terminal)
$ ./sample.py
database: {'Rフランクリン': {'没年': 1957, '名': 'ロザリンド', '注目': 'DNAの発見に貢献', '生年': 1920, '姓': 'フランクリン'}, 'Jグドール': {'没年': None, '注目': '霊長類研究者', '姓': 'ゴドール', '名': 'ジェーン', '著作': ['In the Shadow of Man', 'The Chimpanzees of Gombe'], '生年': 1934}, 'Rカーソン': {'没年': 1964, '注目': 'DDTの毒外に注意を喚起', '姓': 'カーソン', '名': 'レイチェル', '著作': ['沈黙の春'], '生年': 1907}}
database consistent: False
database: {'Jグドール': {'没年': None, '注目': '霊長類研究者', '姓': 'ゴドール', '名': 'ジェーン', '著作': ['In the Shadow of Man', 'The Chimpanzees of Gombe'], '生年': 1934}, 'Rカーソン': {'没年': 1964, '注目': 'DDTの毒外に注意を喚起', '姓': 'カーソン', '名': 'レイチェル', '著作': ['沈黙の春'], '生年': 1907}}
database consistent: True
$
0 コメント:
コメントを投稿