2014年10月12日日曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 11(Storing Data Using Other Collection Types)、11.8(Exercises) 10.を解いてみる。

11.8(Exercises) 10.

コード(BBEdit)

sample10.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def dbHeadings(db):
    result = set()
    
    for value in db.values():
        result.update(value.keys())
        
    return result

def dbConsistent(db):
    flag = False
    
    for value in db.values():
        if not flag:
            keys = value.keys()
            flag = True
        if set(keys) != set(value.keys()):
            return False
                
    return True

if __name__ == '__main__':
    db1 = {
        'jgoodall'  : {'surname'  : 'Goodall',
                       'forename' : 'Jane',
                       'born'     : 1934,
                       'died'     : None,
                       'notes'    : 'primate researcher',
                       'author'   : ['In the Shadow of Man',
                                     'The Chimpanzees of Gombe']},
        'rfranklin' : {'surname'  : 'Franklin',
                       'forename' : 'Rosalind',
                       'born'     : 1920,
                       'died'     : 1957,
                       'notes'    : 'contributed to discovery of DNA'},
        'rcarson'  : {'surname'  : 'Carson',
                      'forename' : 'Rachel',
                      'born'     : 1907,
                      'died'     : 1964,
                      'notes'    : 'raised awareness of effects of DDT',
                      'author'   : ['Silent Spring']}
    }
    db2 = {
        'jgoodall'  : {'surname'  : 'Goodall',
                       'forename' : 'Jane',
                       'born'     : 1934,
                       'died'     : None,
                       'notes'    : 'primate researcher'},
        'rfranklin' : {'surname'  : 'Franklin',
                       'forename' : 'Rosalind',
                       'born'     : 1920,
                       'died'     : 1957,
                       'notes'    : 'contributed to discovery of DNA'},
        'rcarson'  : {'surname'  : 'Carson',
                      'forename' : 'Rachel',
                      'born'     : 1907,
                      'died'     : 1964,
                      'notes'    : 'raised awareness of effects of DDT'}
    }

    print(dbConsistent(db1))
    print(dbConsistent(db2))

入出力結果(Terminal, IPython)

$ ./sample10.py
False
True
$

0 コメント:

コメントを投稿