2020年3月2日月曜日

開発環境

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

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from typing import TextIO
from io import StringIO

print('6.')


class TestSmallestValueSkip(TestCase):
    def test6(self):
        in_file = StringIO('''Example
1
-
3
''')
        self.assertEqual(smallest_value_skip(in_file), 1)


def skip_header(reader: TextIO) -> str:
    _ = reader.readline()
    line = reader.readline()
    while line.startswith('#'):
        line = reader.readline()
    return line


def smallest_value_skip(reader: TextIO) -> int:
    line: str = skip_header(reader)
    smallest: int = int(line)

    for line in reader:
        line = line.strip()
        if line == '-':
            continue
        smallest = min(smallest, int(line))
    return smallest


if __name__ == "__main__":
    main()

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

% ./sample6.py -v
6.
test6 (__main__.TestSmallestValueSkip) ... ok

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

OK
%

0 コメント:

コメントを投稿