開発環境
- 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 11(Storing Data Using Other Collection Types)、Exercise 10、11の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from typing import Any, Dict, Set
print('10, 11.')
class TestDBHeadings(TestCase):
def test_empty(self):
self.assertEqual(db_headings({}), set())
def test_empty1(self):
self.assertEqual(db_headings({'a': {}}), set())
def test_other(self):
self.assertEqual(db_headings({'a': {'b': 1},
'c': {'d': 2, 'e': 3},
'f': {'d': 4}}),
{'b', 'd', 'e'})
class TestDBConsistent(TestCase):
def test_empty(self):
self.assertEqual(db_consistent({}), True)
def test_empty1(self):
self.assertEqual(db_consistent({'a': {}}), True)
def test_false(self):
self.assertEqual(db_consistent({'a': {'b': 1},
'c': {'d': 2, 'e': 3},
'f': {'d': 4}}),
False)
def test_true(self):
self.assertEqual(db_consistent({'a': {'b': 1,
'c': 2},
'd': {'b': 3,
'c': 4},
'e': {'b': 5,
'c': 6}}),
True)
def db_headings(db: Dict[str, Dict[str, Any]]) -> Set[str]:
headings = set()
for value in db.values():
if value:
headings |= set(value.keys())
return headings
def db_consistent(db: Dict[str, Dict[str, Any]]) -> bool:
values = list(db.values())
if not values:
return True
headings = set(values[0])
for value in values[1:]:
if headings != set(value):
return False
return True
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample10.py -v
10, 11.
test_empty (__main__.TestDBConsistent) ... ok
test_empty1 (__main__.TestDBConsistent) ... ok
test_false (__main__.TestDBConsistent) ... ok
test_true (__main__.TestDBConsistent) ... ok
test_empty (__main__.TestDBHeadings) ... ok
test_empty1 (__main__.TestDBHeadings) ... ok
test_other (__main__.TestDBHeadings) ... ok
----------------------------------------------------------------------
Ran 7 tests in 0.000s
OK
%
0 コメント:
コメントを投稿