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) 7.を解いてみる。
11.8(Exercises) 7.
コード(BBEdit)
sample7.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def isBalanced(rgb): balance = sum(rgb.values()) return balance == 1.0 if __name__ == '__main__': import random for x in range(20): rgb = dict(R=random.randrange(1, 5) / 10, G=random.randrange(1, 5) / 10, B=random.randrange(1, 5) / 10) print('RGB({red}, {green}, {blue})): isBalanced: {balanced}'.format( red=rgb['R'], green=rgb['G'], blue=rgb['B'], balanced=isBalanced(rgb)))
入出力結果(Terminal, IPython)
$ ./sample7.py RGB(0.2, 0.1, 0.2): isBalanced: False RGB(0.1, 0.1, 0.3): isBalanced: False RGB(0.1, 0.2, 0.1): isBalanced: False RGB(0.2, 0.1, 0.2): isBalanced: False RGB(0.1, 0.3, 0.4): isBalanced: False RGB(0.1, 0.1, 0.1): isBalanced: False RGB(0.2, 0.4, 0.4): isBalanced: True RGB(0.1, 0.4, 0.2): isBalanced: False RGB(0.1, 0.4, 0.2): isBalanced: False RGB(0.1, 0.1, 0.4): isBalanced: False RGB(0.4, 0.1, 0.3): isBalanced: False RGB(0.1, 0.4, 0.4): isBalanced: False RGB(0.2, 0.1, 0.1): isBalanced: False RGB(0.1, 0.4, 0.2): isBalanced: False RGB(0.4, 0.3, 0.3): isBalanced: True RGB(0.1, 0.2, 0.3): isBalanced: False RGB(0.3, 0.1, 0.4): isBalanced: False RGB(0.2, 0.3, 0.2): isBalanced: False RGB(0.4, 0.3, 0.3): isBalanced: True RGB(0.2, 0.2, 0.2): isBalanced: False $
0 コメント:
コメントを投稿