Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
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) 6.を解いてみる。
11.8(Exercises) 6.
コード(BBEdit)
sample6.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def countDuplicates(d): values = d.values() s1 = set() s2 = set() for value in values: if value in s1: s2.add(value) else: s1.add(value) return len(s2) d1 = dict(a=1, b=2, c=1, d=3, e=4) d2 = dict(a=1, b=2, c=1, d=2, e=3) d3 = dict(a=1, b=2, c=3, d=4, e=5) d4 = dict(a=1, b=2, c=2, d=2, e=3) for d in [d1, d2, d3, d4]: print(countDuplicates(d))
入出力結果(Terminal, IPython)
$ ./sample6.py 1 2 0 1 $
0 コメント:
コメントを投稿