題目

給定下列多種的營業時間格式:

Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Fri - Sun 20:00 - 02:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon, Wed, Fri 20:00 - 02:00
Mon, Wed, Fri 20:00 - 02:00
Mon - Fri 08:00 - 17:00
Mon, Wed, Fri 20:00 - 02:00
Fri - Sun 20:00 - 02:00

請輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
例如: [1, 3, 5]['08:00', '12:00']
代表營業時間為週一、三、五的 08:00 - 12:00
"""

[([1, 3, 5], ['08:00', '12:00']), ([2, 4], ['14:00', '18:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00']), ([6, 7], ['08:00', '12:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00']), ([6, 7], ['08:00', '12:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00']), ([6, 7], ['08:00', '12:00'])]
[([5, 6, 7], ['20:00', '02:00'])]
[([1, 3, 5], ['08:00', '12:00']), ([2, 4], ['14:00', '18:00'])]
[([1, 3, 5], ['08:00', '12:00']), ([2, 4], ['14:00', '18:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00']), ([6, 7], ['08:00', '12:00'])]
[([1, 2, 3], ['08:00', '17:00']), ([4, 6], ['20:00', '02:00'])]
[([1, 2, 3], ['08:00', '17:00']), ([4, 6], ['20:00', '02:00'])]
[([1, 2, 3], ['08:00', '17:00']), ([4, 6], ['20:00', '02:00'])]
[([1, 3, 5], ['08:00', '12:00']), ([2, 4], ['14:00', '18:00'])]
[([1, 3, 5], ['20:00', '02:00'])]
[([1, 3, 5], ['20:00', '02:00'])]
[([1, 2, 3, 4, 5], ['08:00', '17:00'])]
[([1, 3, 5], ['20:00', '02:00'])]
[([5, 6, 7], ['20:00', '02:00'])]


解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

SHORT_WEEK_DICT = {
'Mon': 1,
'Tue': 2,
'Wed': 3,
'Thur': 4,
'Fri': 5,
'Sat': 6,
'Sun': 7,
}


def parse_weeks_and_times(opening_hours):
# 以/符號進行分割 Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
results = []
for item in opening_hours.split('/'):
cols = item.strip().split(' ') # [::-1]

open_and_close_hours = [] # [open_at, close_at]
open_and_close_hours.append(cols.pop())
cols.pop()
open_and_close_hours.append(cols.pop())
open_and_close_hours.reverse()
# open_and_close_time = sorted(open_and_close_hours)

weeks = [] # [1, 2, 3, 4, 5, 6, 7]
if '-' in cols: # 解析區段星期, ['Mon', '-', 'Fri']
for week_id in range(SHORT_WEEK_DICT[cols[0]], 1 + SHORT_WEEK_DICT[cols[-1]]):
weeks.append(week_id)
else: # 解析單一星期, ['Mon,', 'Wed,', 'Fri']
for week in cols:
weeks.append(SHORT_WEEK_DICT[week.replace(',', '')])

results.append((weeks, open_and_close_hours))

return results


rows = """
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Fri 08:00 - 17:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Fri - Sun 20:00 - 02:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon - Fri 08:00 - 17:00 / Sat, Sun 08:00 - 12:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon - Wed 08:00 - 17:00 / Thur, Sat 20:00 - 02:00
Mon, Wed, Fri 08:00 - 12:00 / Tue, Thur 14:00 - 18:00
Mon, Wed, Fri 20:00 - 02:00
Mon, Wed, Fri 20:00 - 02:00
Mon - Fri 08:00 - 17:00
Mon, Wed, Fri 20:00 - 02:00
Fri - Sun 20:00 - 02:00
""".strip().split('\n')

for row in rows:
result = parse_weeks_and_times(row)
print(result)

Keyword

1
2
3
演算法, Algorithm, al-go-rithm
解法, Solution, solu-tion
營業時間, Opening Hours, open-ing hours