## 2018年11月10日土曜日

### Algorithm - Python - パーティーに行くタイミング(タプルのリスト、forループ)

コード(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()
```

```\$ 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