開発環境
- Mac OS X Snow Leopard (OS)
- WingIDE
- Script言語: Python
『初めてのコンピュータサイエンス』(Jennifer Campbell, Paul Gries, Jason Montojo, Greg Wilson 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-463-7)の11章(探索とソート), 11.7(練習問題), 3を解いてみる。
3.
バブルソートの関数
コード
def bubble_sort(L):
for i in range(len(L)):
for j in range(1,len(L)-i):
if L[j]<L[j-1]:
L[j], L[j-1]=L[j-1], L[j]
noseテストケース
コード
import nose
from bubble_sort import bubble_sort
def test_bubble_1():
L=[2,1]
bubble_sort(L)
assert L==[1,2]
def test_bubble_2():
L=[5,4,3,2,1,9,8,7,6,0]
bubble_sort(L)
assert L==[0,1,2,3,4,5,6,7,8,9]
def test_bubble_3():
L=[]
bubble_sort(L)
assert L==[]
def test_bubble_4():
L=[0]
bubble_sort(L)
assert L==[0]
if __name__== '__main__':
nose.runmodule()
入出力結果
Python Shellで何度かエラーを発見しては修正しながら完成!
0 コメント:
コメントを投稿