2018年11月10日土曜日

開発環境

問題解決のPythonプログラミング ―数学パズルで鍛えるアルゴリズム的思考 (Srini Devadas (著)、黒川 利明 (翻訳)、オライリージャパン)の2章(パーティーに行くタイミング)、練習問題、問題1.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
''' sample '''


def best_time_to_party_smart(schedule: list, ystart: float, yend: float) -> int:
    '''
    >>> best_time_to_party_smart([], 7, 8)
    0
    >>> best_time_to_party_smart([(6, 7)], 7, 8)
    0
    >>> best_time_to_party_smart([(6.5, 7.5)], 7, 8)
    1
    >>> best_time_to_party_smart([(7, 8)], 7, 8)
    1
    >>> best_time_to_party_smart([(7.5, 8.5)], 7, 8)
    1
    >>> best_time_to_party_smart([(8, 9)], 7, 8)
    0
    >>> best_time_to_party_smart([(6, 7), (6.5, 7.5)], 7, 8)
    1
    >>> best_time_to_party_smart([(6.5, 7.5), (7, 8)], 7, 8)
    2
    >>> best_time_to_party_smart([(7, 8), (8, 9)], 7, 8)
    1
    >>> best_time_to_party_smart([(8, 9), (8.5, 9.5)], 7, 8)
    0
    '''
    count = 0
    for start, end in schedule:
        if start < ystart < end or \
           ystart <= start < yend:
            count += 1
    return count


if __name__ == '__main__':
    import doctest
    doctest.testmod()

入出力結果(Terminal, Jupyter(IPython))

$ pylint sample1.py
************* Module sample1
sample1.py:1:0: C0111: Missing module docstring (missing-docstring)

-------------------------------------------------------------------
Your code has been rated at 8.89/10

$ pylint sample1.py

-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 8.89/10, +1.11)

$ ./sample1.py
Trying:
    best_time_to_party_smart([], 7, 8)
Expecting:
    0
ok
Trying:
    best_time_to_party_smart([(6, 7)], 7, 8)
Expecting:
    0
ok
Trying:
    best_time_to_party_smart([(6.5, 7.5)], 7, 8)
Expecting:
    1
ok
Trying:
    best_time_to_party_smart([(7, 8)], 7, 8)
Expecting:
    1
ok
Trying:
    best_time_to_party_smart([(7.5, 8.5)], 7, 8)
Expecting:
    1
ok
Trying:
    best_time_to_party_smart([(8, 9)], 7, 8)
Expecting:
    0
ok
Trying:
    best_time_to_party_smart([(6, 7), (6.5, 7.5)], 7, 8)
Expecting:
    1
ok
Trying:
    best_time_to_party_smart([(6.5, 7.5), (7, 8)], 7, 8)
Expecting:
    2
ok
Trying:
    best_time_to_party_smart([(7, 8), (8, 9)], 7, 8)
Expecting:
    1
ok
Trying:
    best_time_to_party_smart([(8, 9), (8.5, 9.5)], 7, 8)
Expecting:
    0
ok
1 items had no tests:
    __main__
1 items passed all tests:
  10 tests in __main__.best_time_to_party_smart
10 tests in 2 items.
10 passed and 0 failed.
Test passed.
$

0 コメント:

コメントを投稿

関連コンテンツ