2019年10月26日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 7(Using Methods)、Exercise 2、3、4、5、6、7、8の解答を求めてみる。

コード

Python 3

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


class MyTestCase(TestCase):
    def test2(self):
        self.assertEqual('tomato'.count('o'), 2)

    def test3(self):
        self.assertEqual('tomato'.find('o'), 1)

    def test4(self):
        self.assertEqual('tomato'.find('o', 'tomato'.find('o') + 1), 5)

    def test5(self):
        self.assertEqual('avogado'.find('o', 'avogado'.find('o') + 1), 6)

    def test6(self):
        self.assertEqual('runner'.replace('n', 'b'), 'rubber')

    def test7(self):
        s = ' yes  '
        self.assertEqual(str.strip(s), 'yes')
        self.assertEqual(s.strip(), 'yes')

    def test8a(self):
        fruit = 'pineapple'
        self.assertEqual(fruit.count('p'), 3)
        self.assertEqual(fruit.find('p', 3), 5)
        self.assertEqual(fruit.find('p', fruit.count('p')), 5)

    def test8b(self):
        fruit = 'pineapple'
        self.assertEqual(fruit.upper(), 'PINEAPPLE')
        self.assertEqual(fruit.upper().swapcase(), 'pineapple')
        self.assertEqual(fruit.count(fruit.upper().swapcase()), 1)

    def test8c(self):
        fruit = 'pineapple'
        self.assertEqual(fruit.swapcase(), 'PINEAPPLE')
        self.assertEqual(fruit.lower(), 'pineapple')
        self.assertEqual(fruit.replace(
            fruit.swapcase(), fruit.lower()), 'pineapple')


if __name__ == '__main__':
    main()

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

% ./sample2.py -v
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok
test4 (__main__.MyTestCase) ... ok
test5 (__main__.MyTestCase) ... ok
test6 (__main__.MyTestCase) ... ok
test7 (__main__.MyTestCase) ... ok
test8a (__main__.MyTestCase) ... ok
test8b (__main__.MyTestCase) ... ok
test8c (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK
%

0 コメント:

コメントを投稿

関連コンテンツ