開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を6章(テキスト、文字列、ヒューリスティック - すべてを組み合わせる)の自分で考えてみよう(275ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
from unittest import TestCase, main
class MyTestCase(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test(self):
words = ['', 'a', 'e', 'aa', 'ae', 'ea', 'ee', 'aaa',
'aae', 'aea', 'aee', 'eaa', 'eae', 'eea', 'eee']
processed_words = ['', 'a', '', 'aa', 'a', 'ea', 'e', 'aaa',
'aa', 'aea', 'ae', 'eaa', 'ea', 'eea', 'ee']
for word, processed_word in zip(words, processed_words):
if not word:
self.assertEqual(word, processed_word)
else:
if word[-1] in 'eE':
word = word[:-1]
self.assertEqual(word, processed_word)
if __name__ == '__main__':
main()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample3.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿