開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 7(Using Methods)、Exercise 12の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 import doctest def total_occurrences(s1: str, s2: str, ch: str) -> int: ''' >>> total_occurrences('color', 'yellow', 'l') 3 >>> total_occurrences('red', 'blue', 'l') 1 >>> total_occurrences('green', 'purple', 'b') 0 ''' if len(ch) != 1: raise ValueError(f'len("{ch}") want 1, got {len(ch)}') return s1.count(ch) + s2.count(ch) if __name__ == '__main__': doctest.testmod()
入出力結果(Zsh、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
% ./sample12.py -v Trying: total_occurrences('color', 'yellow', 'l') Expecting: 3 ok Trying: total_occurrences('red', 'blue', 'l') Expecting: 1 ok Trying: total_occurrences('green', 'purple', 'b') Expecting: 0 ok 1 items had no tests: __main__ 1 items passed all tests: 3 tests in __main__.total_occurrences 3 tests in 2 items. 3 passed and 0 failed. Test passed. %
0 コメント:
コメントを投稿