-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_match_case.py
More file actions
27 lines (20 loc) · 1.26 KB
/
Copy path02_match_case.py
File metadata and controls
27 lines (20 loc) · 1.26 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
"""
Тема: match/case, structural pattern matching (см. theory/python.md → «Что нового в Python»).
Реализуй describe_event(event) через match/case. На вход приходят разные по форме
события (dict-ы), надо вернуть человекочитаемую строку:
- {"type": "click", "x": .., "y": ..} -> "click at (x, y)"
- {"type": "key", "key": ..} -> "key <key>"
- {"type": "scroll", "delta": ..} где delta > 0 -> "scroll up"
- {"type": "scroll", "delta": ..} где delta < 0 -> "scroll down"
- всё остальное -> "unknown event"
Используй именно match/case со структурными паттернами (маппинги + guard),
а не цепочку if/elif по ключам.
"""
def describe_event(event: dict) -> str:
raise NotImplemented
assert describe_event({"type": "click", "x": 10, "y": 20}) == "click at (10, 20)"
assert describe_event({"type": "key", "key": "Enter"}) == "key Enter"
assert describe_event({"type": "scroll", "delta": 3}) == "scroll up"
assert describe_event({"type": "scroll", "delta": -2}) == "scroll down"
assert describe_event({"type": "resize"}) == "unknown event"
print('ok')