File size: 1,209 Bytes
3778698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from collections import defaultdict
from typing import Dict, List

def merge_schedules(entries: List[dict]) -> Dict[str, List[dict]]:
    """Group simple schedule entries by day of week.

    Each entry is expected to have:
      - `days`: comma-separated days like "mon,tue"
      - `time_local`
      - `zone_id`
    Returns a mapping day -> list of small dicts with time and zone.
    """
    result: Dict[str, List[dict]] = defaultdict(list)

    for e in entries:
        days_field = str(e.get("days", "")).strip()
        time_local = str(e.get("time_local", "00:00"))
        zone_id = str(e.get("zone_id", ""))

        if not days_field:
            continue

        days = [d.strip() for d in days_field.split(",") if d.strip()]
        for d in days:
            result[d].append({"time_local": time_local, "zone_id": zone_id})

    # sort each day's list by time
    for d in result:
        result[d].sort(key=lambda x: x["time_local"])

    return dict(result)

if __name__ == "__main__":
    entries = [
        {"days": "mon,wed", "time_local": "09:00", "zone_id": "LR_CENTER"},
        {"days": "mon", "time_local": "20:00", "zone_id": "KT_COOK"},
    ]
    print(merge_schedules(entries))