This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| class APIError(Exception): | |
| pass | |
| def fetch_cat_fact(): | |
| url = "https://catfact.ninja/fact" | |
| headers = {"accept": "application/json"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def process_events(events: list[tuple[int, str]], window=10): | |
| last_seen = {} | |
| result = [] | |
| for timestamp, user_id in events: | |
| if user_id not in last_seen: | |
| result.append(True) | |
| last_seen[user_id] = timestamp | |
| else: | |
| if timestamp - last_seen[user_id] >= window: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_pairs(arr: list, target: int): | |
| if not arr or len(arr) < 2: | |
| return [] | |
| # x+y=target | |
| # y=target-x | |
| seen = set() | |
| result = [] | |
| for x_num in arr: |