開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 5(Making Choices)、Exercises 3、4、5、6の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 from unittest import TestCase, main class MyTestCase(TestCase): def setUp(self): pass def tearDown(self): pass def test3(self): def f(full, empty): return (full and not empty) or (not full and empty) self.assertFalse(f(True, True)) self.assertTrue(f(True, False)) self.assertTrue(f(False, True)) self.assertFalse(f(False, False)) def test4(self): # right def f(light, temperature): return ((light < 0.01) or (temperature > 0.0)) and \ (not ((light < 0.01) and (temperature > 0.0))) def g(light, temperature): return (light < 0.01) != (temperature > 0.0) for light in [0.0, 0.01]: for temperature in [0.1, 0.0]: self.assertEqual(f(light, temperature), g(light, temperature)) def test5(self): def f(x): x == abs(x) return x == abs(x) self.assertTrue(f(1)) self.assertFalse(f(-1)) def test6(self): def different(a, b): return a != b self.assertTrue(different(1, 2)) self.assertFalse(different(1, 1)) if __name__ == '__main__': main()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ./sample3.py -v test3 (__main__.MyTestCase) ... ok test4 (__main__.MyTestCase) ... ok test5 (__main__.MyTestCase) ... ok test6 (__main__.MyTestCase) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.000s OK $
0 コメント:
コメントを投稿