2019年7月13日土曜日

開発環境

Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を8章(再帰と辞書 - 反復とインデックスを超えて)の自分で考えてみよう(347ページ)の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main


class SampleTestCase(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_empty(self):
        self.assertTrue(palindrome(''))

    def test_one(self):
        self.assertTrue(palindrome(' '))
        self.assertTrue(palindrome('a'))

    def test_two(self):
        self.assertTrue(palindrome('aa'))
        self.assertFalse(palindrome('ab'))

    def test_three(self):
        self.assertTrue(palindrome('aaa'))
        self.assertFalse(palindrome('aab'))
        self.assertTrue(palindrome('aba'))
        self.assertFalse(palindrome('abb'))
        self.assertFalse(palindrome('baa'))
        self.assertTrue(palindrome('bab'))
        self.assertFalse(palindrome('bba'))
        self.assertTrue(palindrome('bbb'))

    def test_five(self):
        self.assertTrue(palindrome('abcba'))
        self.assertFalse(palindrome('abcbb'))

    def test_six(self):
        self.assertTrue(palindrome('abccba'))
        self.assertFalse(palindrome('abccbb'))


def palindrome(s: str) -> bool:
    if not s:
        return True
    l = len(s)
    if l == 1:
        return True
    if s[0] == s[-1]:
        return palindrome(s[1:-1])
    return False


if __name__ == '__main__':
    main()

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

$ ./sample1.py
EEEEEE
======================================================================
ERROR: test_empty (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 13, in test_empty
    self.assertTrue(palindrome(''))
NameError: name 'palindrome' is not defined

======================================================================
ERROR: test_five (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 34, in test_five
    self.assertTrue(palindrome('abcba'))
NameError: name 'palindrome' is not defined

======================================================================
ERROR: test_one (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 16, in test_one
    self.assertTrue(palindrome(' '))
NameError: name 'palindrome' is not defined

======================================================================
ERROR: test_six (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 38, in test_six
    self.assertTrue(palindrome('abccba'))
NameError: name 'palindrome' is not defined

======================================================================
ERROR: test_three (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 24, in test_three
    self.assertTrue(palindrome('aaa'))
NameError: name 'palindrome' is not defined

======================================================================
ERROR: test_two (__main__.SampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 20, in test_two
    self.assertTrue(palindrome('aa'))
NameError: name 'palindrome' is not defined

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (errors=6)
$ ./sample1.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.000s

OK
$ 

0 コメント:

コメントを投稿