2020年3月12日木曜日

開発環境

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 1、2の解答を求めてみる。

コード

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

print('1, 2.')


class TestFindDups(TestCase):
    def test_empty(self):
        self.assertEqual(find_dups([]), set())

    def test_one(self):
        self.assertEqual(find_dups([1]), set())

    def test_one_dup(self):
        self.assertEqual(find_dups([0, 1, 1]), {1})

    def test_two_dup(self):
        self.assertEqual(find_dups([0, 1, 2, 3, 4, 5, 1, 2, 1, 2, 1]),
                         {1, 2})


class TestReadMolecule(TestCase):
    def test(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(read_molecule(infile),
                         {'TEST': [('N', ('0.1', '0.2', '0.3')),
                                   ('N', ('0.2', '0.1', '0.0'))]})


class TestReadAllMolecules(TestCase):
    def test(self):
        cmpnd1 = '''COMPND T1
ATOM 1 N 0.1 0.2 0.3
ATOM 2 N 0.2 0.1 0.0
END
'''
        cmpnd2 = '''COMPND T2
ATOM 1 A 0.1 0.2 0.3
ATOM 2 A 0.2 0.1 0.0
END
'''
        infile = StringIO(cmpnd1 + cmpnd2)
        self.assertEqual(read_all_molecules(infile),
                         {'T1': [('N', ('0.1', '0.2', '0.3')),
                                 ('N', ('0.2', '0.1', '0.0'))],
                          'T2': [('A', ('0.1', '0.2', '0.3')),
                                 ('A', ('0.2', '0.1', '0.0'))]})


def find_dups(nums: List[int]) -> Set[int]:
    s1: Set = set()
    s2: Set = set()
    for num in nums:
        if num in s1:
            s2.add(num)
        s1.add(num)
    return s2


Atom = Tuple[str, Tuple[str, str, str]]
CompoundDict = Dict[str, List[Atom]]


def read_molecule(reader: TextIO) -> Union[CompoundDict, None]:
    line = reader.readline()
    if not line:
        return None
    parts: List[str] = line.split()
    name: str = parts[1]
    molecule: CompoundDict = {name: []}
    for line in reader:
        if line.startswith('END'):
            break
        parts = line.split()
        molecule[name].append((parts[2], (parts[3], parts[4], parts[5])))
    return molecule


def read_all_molecules(reader: TextIO) -> CompoundDict:
    result = {}
    while True:
        molecule = read_molecule(reader)
        if molecule is None:
            break
        else:
            name = list(molecule.keys())[0]
            result[name] = list(molecule.values())[0]
    return result


if __name__ == "__main__":
    main()

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

% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py -v
1, 2.
test_empty (__main__.TestFindDups) ... ok
test_one (__main__.TestFindDups) ... ok
test_one_dup (__main__.TestFindDups) ... ok
test_two_dup (__main__.TestFindDups) ... ok
test (__main__.TestReadAllMolecules) ... ok
test (__main__.TestReadMolecule) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK
%

0 コメント:

コメントを投稿