-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_16.py
More file actions
77 lines (68 loc) · 2.48 KB
/
task_16.py
File metadata and controls
77 lines (68 loc) · 2.48 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
# You're building a tool that tracks component edits and groups them into a changelog.
# Given an array of edit actions, each with a timestamp and a component name, return an array
# of grouped changelog entries. Edits to the same component within a 10-minute window should be
# merged into one changelog entry, showing the component name and the range of timestamps affected.
# Example:
# const edits = [
# { timestamp: "2025-10-06T08:00:00Z", component: "Header" },
# { timestamp: "2025-10-06T08:05:00Z", component: "Header" },
# { timestamp: "2025-10-06T08:20:00Z", component: "Header" },
# { timestamp: "2025-10-06T08:07:00Z", component: "Footer" },
# { timestamp: "2025-10-06T08:15:00Z", component: "Footer" },
# ];
# > groupChangelogEdits(edits)
# > [
# {
# "component": "Footer",
# "start": "2025-10-06T08:07:00Z",
# "end": "2025-10-06T08:15:00Z"
# },
# {
# "component": "Header",
# "start": "2025-10-06T08:00:00Z",
# "end": "2025-10-06T08:05:00Z"
# },
# {
# "component": "Header",
# "start": "2025-10-06T08:20:00Z",
# "end": "2025-10-06T08:20:00Z"
# }
# ]
from datetime import datetime, timedelta
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
TIME_DELTA = timedelta(minutes=10)
def group_changelog_edits(components):
result = []
for element in components:
existing = next(
(
el
for el in result
if el.get("component") == element.get("component")
and abs(
datetime.strptime(el.get("start"), TIME_FORMAT)
- datetime.strptime(element.get("timestamp"), TIME_FORMAT)
)
<= TIME_DELTA
),
None,
)
if not existing:
result.append(
{
"component": element.get("component"),
"start": element.get("timestamp"),
"end": element.get("timestamp"),
}
)
else:
existing["end"] = element.get("timestamp")
return result
components = [
{"timestamp": "2025-10-06T08:00:00Z", "component": "Header"},
{"timestamp": "2025-10-06T08:05:00Z", "component": "Header"},
{"timestamp": "2025-10-06T08:20:00Z", "component": "Header"},
{"timestamp": "2025-10-06T08:07:00Z", "component": "Footer"},
{"timestamp": "2025-10-06T08:15:00Z", "component": "Footer"},
]
print(group_changelog_edits(components))