## 2019年11月8日金曜日

### Python - Storing Collections of Data Using Lists - sort method(ascending order), slicing, list arithmetic(addition)

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 8(Storing Collections of Data Using Lists)、Exercise 6の解答を求めてみる。

コード

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

temps = [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6]
cool_temps = None
warm_temps = None

class MyTestCase(TestCase):
def test_b(self):
temps.sort()
self.assertEqual(temps, [16.8, 19.6, 22.5, 23.9, 25.2, 28, 31.4])

def test_c(self):
global cool_temps, warm_temps
cool_temps = temps[:2]
warm_temps = temps[2:]
self.assertEqual(cool_temps, [16.8, 19.6])
self.assertEqual(warm_temps, [22.5, 23.9, 25.2, 28, 31.4])

def test_d(self):
global cool_temps, warm_temps
temps_in_celsius = cool_temps + warm_temps
self.assertEqual(temps_in_celsius, temps)

if __name__ == '__main__':
main()

% ./sample6.py -v
test_b (__main__.MyTestCase) ... ok
test_c (__main__.MyTestCase) ... ok
test_d (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
%