## 2020年3月13日金曜日

### Python - Storing Data Using Other Collection Types - Set(要素数(len関数)、popメソッド)、Tuple、Dictionary(itmesメソッド)

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 11(Storing Data Using Other Collection Types)、Exercise 3、4、5、6の解答を求めてみる。

コード

```#!/usr/bin/env python3
from unittest import TestCase, main
from typing import Any, List, Set, Tuple, Dict, TextIO
from io import StringIO

print('3, 4, 5, 6.')

class TestMatingPairs(TestCase):
def test_empty(self):
self.assertEqual(mating_pairs(set(), set()), set())

def test_one(self):
self.assertEqual(mating_pairs({1}, {2}), {(1, 2)})

def test_other(self):
males = {0, 2}
females = {1, 3}
pairs = mating_pairs(males, females)
self.assertTrue(pairs == {(0, 1), (2, 3)} or
pairs == {(0, 3), (2, 1)})

class TestAuthorsFromFile(TestCase):
def test_empty(self):
instring = '''COMPND TEST
ATOM 1 N 0.1 0.2 0.3
ATOM 2 N 0.2 0.1 0.0
END
'''
infile = StringIO(instring)
self.assertEqual(get_authors_from_file(infile), set())

def test_one(self):
instring = '''AUTHOR author name
COMPND TEST
ATOM 1 N 0.1 0.2 0.3
ATOM 2 N 0.2 0.1 0.0
END
'''
infile = StringIO(instring)
self.assertEqual(get_authors_from_file(infile), {'author name'})

def test_other(self):
instring = '''AUTHOR author name
COMPND TEST
ATOM 1 N 0.1 0.2 0.3
ATOM 2 N 0.2 0.1 0.0
AuTHor author name1

author author name

END
'''
infile = StringIO(instring)
self.assertEqual(get_authors_from_file(infile),
{'author name', 'author name1'})

class TestDistinctValues(TestCase):
def test(self):
self.assertEqual(coun_values({'red': 1, 'green': 1, 'blue': 2}), 2)

class TestLeastLikelyObserved(TestCase):
def test(self):
self.assertEqual(least_likely_observed(
{'neutron': 0.55, 'proton': 0.21, 'meson': 0.03, 'meson': 0.03,
'muon': 0.07, 'neutrino': 0.14}),
'meson')

def mating_pairs(males: set, females: set) -> Set[Tuple[Any, Any]]:
pairs = set()
for _ in range(len(males)):
return pairs

def get_authors_from_file(reader: TextIO) -> Set[str]:
return {line[6:].strip()
for line in reader if line.lower().startswith('author')}

def get_authors(filename: str) -> Set[str]:
with open(filename) as f:
authors = get_authors_from_file(f)
return authors

def coun_values(d: dict) -> int:
return len(set(d.values()))

def least_likely_observed(d: Dict[str, int]) -> str:
s = ''
m = 1
for k, v in d.items():
if v < m:
s = k
m = v
return s

if __name__ == "__main__":
main()
```

```% ./sample3.py -v
3, 4, 5, 6.
test_empty (__main__.TestAuthorsFromFile) ... ok
test_one (__main__.TestAuthorsFromFile) ... ok
test_other (__main__.TestAuthorsFromFile) ... ok
test (__main__.TestDistinctValues) ... ok
test (__main__.TestLeastLikelyObserved) ... ok
test_empty (__main__.TestMatingPairs) ... ok
test_one (__main__.TestMatingPairs) ... ok
test_other (__main__.TestMatingPairs) ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.001s

OK
%
```