Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Yosemite - 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 13(Searching and Sorting)、13.7(Exercises), 5-a, b, c, d.を解いてみる。
13.7(Exercises), 5-a, b, c, d.
コード(BBEdit)
sample5.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def bubbleSort(l): ''' (list) -> None >>> l = [5, 1, 4, 2, 3] >>> bubbleSort(l) >>> l [1, 2, 3, 4, 5] >>> l = [5] >>> bubbleSort(l) >>> l [5] >>> l = [] >>> bubbleSort(l) >>> l [] >>> l = [5, 5] >>> bubbleSort(l) >>> l [5, 5] >>> l = [5, 1, 4, 2, 3, 2] >>> bubbleSort(l) >>> l [1, 2, 2, 3, 4, 5] ''' i = 0 m = len(l) - 1 while m > 0: while i < m: if l[i] > l[i + 1]: l[i + 1], l[i] = l[i], l[i + 1] i += 1 i = 0 m -= 1 if __name__ == '__main__': import doctest doctest.testmod()
入出力結果(Terminal, IPython)
$ ./sample5.py -v Trying: l = [5, 1, 4, 2, 3] Expecting nothing ok Trying: bubbleSort(l) Expecting nothing ok Trying: l Expecting: [1, 2, 3, 4, 5] ok Trying: l = [5] Expecting nothing ok Trying: bubbleSort(l) Expecting nothing ok Trying: l Expecting: [5] ok Trying: l = [] Expecting nothing ok Trying: bubbleSort(l) Expecting nothing ok Trying: l Expecting: [] ok Trying: l = [5, 5] Expecting nothing ok Trying: bubbleSort(l) Expecting nothing ok Trying: l Expecting: [5, 5] ok Trying: l = [5, 1, 4, 2, 3, 2] Expecting nothing ok Trying: bubbleSort(l) Expecting nothing ok Trying: l Expecting: [1, 2, 2, 3, 4, 5] ok 1 items had no tests: __main__ 1 items passed all tests: 15 tests in __main__.bubbleSort 15 tests in 2 items. 15 passed and 0 failed. Test passed. $
0 コメント:
コメントを投稿