開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.8 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 13(Searching and Sorting)、Exercise 5の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
print('5.')
class TestBubbleSort(TestCase):
def test_empty(self):
l = []
bubble_sort(l)
self.assertEqual(l, [])
def test_one(self):
l = [1]
bubble_sort(l)
self.assertEqual(l, [1])
def test_descent(self):
l = [2, 1]
bubble_sort(l)
self.assertEqual(l, [1, 2])
def test_ascent(self):
l = [1, 2]
bubble_sort(l)
self.assertEqual(l, [1, 2])
def test_duplicate(self):
l = [3, 3, 3]
bubble_sort(l)
self.assertEqual(l, [3, 3, 3])
def test_general(self):
l = [3, 4, 7, -1, 2, 5]
bubble_sort(l)
self.assertEqual(l, [-1, 2, 3, 4, 5, 7])
def test_general_duplicate(self):
l = [-5, 3, 0, 3, -6, 2, 1, 1]
bubble_sort(l)
self.assertEqual(l, [-6, -5, 0, 1, 1, 2, 3, 3])
def bubble_sort(l: list) -> None:
last_index: int = len(l) - 1
while last_index > 0:
for i in range(last_index):
if l[i] > l[i + 1]:
l[i], l[i + 1] = l[i + 1], l[i]
last_index -= 1
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample5.py -v
5.
test_ascent (__main__.TestBubbleSort) ... ok
test_descent (__main__.TestBubbleSort) ... ok
test_duplicate (__main__.TestBubbleSort) ... ok
test_empty (__main__.TestBubbleSort) ... ok
test_general (__main__.TestBubbleSort) ... ok
test_general_duplicate (__main__.TestBubbleSort) ... ok
test_one (__main__.TestBubbleSort) ... ok
----------------------------------------------------------------------
Ran 7 tests in 0.000s
OK
%
0 コメント:
コメントを投稿