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 9(Repeating Code Using Loops)、9.10(Exercises) 12.を解いてみる。
9.10(Exercises) 12.
コード(BBEdit)
sample12.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def remove_neg(num_list): """ (list of number) -> NontType Remove the negative numbers from the list num_list >>> numbers = [-5, 1, -3, 2] >>> remove_neg(numbers) >>> numbers [1, 2] >>> numbers = [-1, -3] >>> remove_neg(numbers) >>> numbers [] """ i = 0 while i < len(num_list): if num_list[i] < 0: num_list.remove(num_list[i]) else: i += 1 if __name__ == '__main__': import doctest doctest.testmod()
入出力結果(Terminal, IPython)
$ ./sample12.py -v Trying: numbers = [-5, 1, -3, 2] Expecting nothing ok Trying: remove_neg(numbers) Expecting nothing ok Trying: numbers Expecting: [1, 2] ok Trying: numbers = [-1, -3] Expecting nothing ok Trying: remove_neg(numbers) Expecting nothing ok Trying: numbers Expecting: [] ok 1 items had no tests: __main__ 1 items passed all tests: 6 tests in __main__.remove_neg 6 tests in 2 items. 6 passed and 0 failed. Test passed. $
0 コメント:
コメントを投稿