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 7(Using Methods)、7.6(Exercises) 12.を解いてみる。
7.6(Exercises) 12.
コード(BBEdit)
sample12.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def total_occurences(s1, s2, ch): """ (str, str, str) -> int Precondition: len(ch) == 1 Return the total number of times that ch occurs in s1 and s2 >>> total_occurences('color', 'yellow', 'l') 3 >>> total_occurences('red', 'blue', 'l') 1 >>> total_occurences('green', 'purple', 'b') 0 """ occurences = 0 for x in s1 + s2: if x == ch: occurences += 1 return occurences if __name__ == '__main__': import doctest doctest.testmod()
入出力結果(Terminal, IPython)
$ ./sample12.py -v Trying: total_occurences('color', 'yellow', 'l') Expecting: 3 ok Trying: total_occurences('red', 'blue', 'l') Expecting: 1 ok Trying: total_occurences('green', 'purple', 'b') Expecting: 0 ok 1 items had no tests: __main__ 1 items passed all tests: 3 tests in __main__.total_occurences 3 tests in 2 items. 3 passed and 0 failed. Test passed. $
0 コメント:
コメントを投稿