2019年9月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 8の解答を求めてみる。

コード

Python 3

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


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

    def tearDown(self):
        pass

    def test_a(self):
        units = ['Kelvin', 'Fahrenheit', 'Rankine', 'Delisle', 'Newton',
                 'Reaumur', 'Romer']
        temperatures = [0, -459.67, 0.00, 559.73, -90.14, -218.52, -135.9]
        for unit, temperature in zip(units, temperatures):
            self.assertAlmostEqual(
                convert_temperatures(temperature, unit, 'Celsius'),
                -273.15, places=1)
            self.assertAlmostEqual(
                convert_temperatures(-273.15, 'Celsius', unit),
                temperature, places=1)


def convert_temperatures(t: float, source: str, target: str) -> float:
    if source == 'Kelvin':
        celsius: float = t - 273.15
    elif source == 'Celsius':
        celsius = t
    elif source == 'Fahrenheit':
        celsius = (t - 32) * 5 / 9
    elif source == 'Rankine':
        celsius = (t - 491.67) * 5/9
    elif source == 'Delisle':
        celsius = 100 - t * 2 / 3
    elif source == 'Newton':
        celsius = t * 100 / 33
    elif source == 'Reaumur':
        celsius = t * 5 / 4
    elif source == 'Romer':
        celsius = (t - 7.5) * 40 / 21
    else:
        raise ValueError(f'Invalid source: {source}')

    if target == 'Kelvin':
        temperature: float = celsius + 273.15
    elif target == 'Celsius':
        temperature = celsius
    elif target == 'Fahrenheit':
        temperature = celsius * 9 / 5 + 32
    elif target == 'Rankine':
        temperature = (celsius + 273.15) * 9/5
    elif target == 'Delisle':
        temperature = (100 - celsius) * 3 / 2
    elif target == 'Newton':
        temperature = celsius * 33 / 100
    elif target == 'Reaumur':
        temperature = celsius * 4 / 5
    elif target == 'Romer':
        temperature = celsius * 21 / 40 + 7.5
    else:
        raise ValueError(f'Invalid target: {target}')

    return temperature


if __name__ == '__main__':
    main()

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

$ ./sample8.py
E
======================================================================
ERROR: test_a (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample8.py", line 18, in test_a
    convert_temperatures(temperature, unit, 'Celsius'),
NameError: name 'convert_temperatures' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
$ ./sample8.py
F
======================================================================
FAIL: test_a (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample8.py", line 22, in test_a
    temperature)
AssertionError: -459.66999999999996 != -459.67

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
$ ./sample8.py
F
======================================================================
FAIL: test_a (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample8.py", line 19, in test_a
    -273.15)
AssertionError: -273.15333333333336 != -273.15 within 7 places (0.003333333333387145 difference)

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
$ ./sample8.py
F
======================================================================
FAIL: test_a (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample8.py", line 22, in test_a
    temperature, places=2)
AssertionError: 559.7249999999999 != 559.73 within 2 places (0.005000000000109139 difference)

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
$ ./sample8.py
F
======================================================================
FAIL: test_a (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample8.py", line 19, in test_a
    -273.15, places=1)
AssertionError: -0.1707142857142857 != -273.15 within 1 places (272.9792857142857 difference)

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
$ ./sample8.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
$ 

b. Two.

0 コメント:

コメントを投稿