2020年7月5日日曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 15(Testing and Debugging)、Exercise 1の解答を求めてみる。

コード

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


class TestDoublePreceding(TestCase):
    def test_empty(self):
        l = []
        double_preceding(l)
        self.assertEqual(l, [])

    def test_one(self):
        l = [1]
        double_preceding(l)
        self.assertEqual(l, [0])

    def test_three(self):
        l = [1, 2, 3]
        double_preceding(l)
        self.assertEqual(l, [0, 2, 4])

    def test_three1(self):
        l = [1, 3, 5]
        double_preceding(l)
        self.assertEqual(l, [0, 2, 6])


def double_preceding(l: List[float]):
    if l != []:
        t = l[0]
        l[0] = 0
        # for i in range(1, len(l)):
        #     v = 2 * t
        #     t = l[i]
        #     l[i] = v
        for i, v in enumerate(l[1:], 1):
            l[i] = 2 * t
            t = v


if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample1.py
...F
======================================================================
FAIL: test_three1 (__main__.TestDoublePreceding)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 25, in test_three1
    self.assertEqual(l, [0, 2, 6])
AssertionError: Lists differ: [0, 2, 4] != [0, 2, 6]

First differing element 2:
4
6

- [0, 2, 4]
?        ^

+ [0, 2, 6]
?        ^


----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=1)
% ./sample1.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% ./sample1.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% ./sample1.py -v
test_empty (__main__.TestDoublePreceding) ... ok
test_one (__main__.TestDoublePreceding) ... ok
test_three (__main__.TestDoublePreceding) ... ok
test_three1 (__main__.TestDoublePreceding) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% 

0 コメント:

コメントを投稿