Welcome to your Python problem sets! These exercises will help you build real-world coding skills used by software developers. Each problem focuses on a specific skill, like parsing logs or processing data, and is designed to be solved in about 20 minutes. You'll get a detailed explanation, starter code, and test cases to guide you. Let's get started!
Logs are like a diary for apps, recording what happens (e.g., errors or user actions). Developers often need to parse log files to extract useful information, like timestamps or error messages.
Write a function parse_log_entry that parses a log entry string with the format "[TIMESTAMP] LEVEL: MESSAGE". Extract the timestamp, level, and message into a dictionary. Return None if the entry is invalid (e.g., wrong level or format).
- Input: A string
log_entry(e.g., "[2025-07-29 09:06:00] INFO: User logged in"). - Output: A dictionary with
timestamp,level, andmessage, orNoneif invalid. - Constraints:
- Valid levels: "INFO", "ERROR", "WARNING".
- Timestamp format: "[YYYY-MM-DD HH:MM:SS]".
- Input string length: 0 to 100 characters.
def parse_log_entry(log_entry):
# Your code here
passprint(parse_log_entry("[2025-07-29 09:06:00] INFO: User logged in"))
# {"timestamp": "2025-07-29 09:06:00", "level": "INFO", "message": "User logged in"}
print(parse_log_entry("[2025-07-29 09:06:00] DEBUG: Test")) # None
print(parse_log_entry("invalid")) # None
print(parse_log_entry("[2025-07-29 10:00:00] ERROR: Failed login"))
# {"timestamp": "2025-07-29 10:00:00", "level": "ERROR", "message": "Failed login"}Apps often use configuration files to store settings, like database addresses or ports. Parsing these files correctly is a common task for developers setting up or maintaining software.
Write a function read_config that parses a configuration string where each line is a "key=value" pair. Ignore empty lines, comments (starting with #), or invalid lines. Return a dictionary of key-value pairs.
- Input: A string
configwith multiple lines (e.g., "host=localhost\nport=8080\n#comment"). - Output: A dictionary with key-value pairs.
- Constraints:
- Keys and values: strings, 1 to 20 characters.
- Input lines: 0 to 50.
- Ignore malformed lines (e.g., no "=").
def read_config(config):
# Your code here
passprint(read_config("host=localhost\nport=8080\n#comment\ninvalid"))
# {"host": "localhost", "port": "8080"}
print(read_config("")) # {}
print(read_config("name=Alice\nname=Bob")) # {"name": "Bob"}
print(read_config("key= value \n#test")) # {"key": "value"}Apps like task schedulers or job queues prioritize tasks based on importance (e.g., sending urgent emails first). Processing tasks in the right order is a key skill for backend developers.
Write a function process_tasks that takes a list of task dictionaries (with id and priority fields) and processes up to 3 tasks with the highest priority (largest number). Return a list of their IDs.
- Input: A list of task dictionaries, e.g.,
[{"id": 1, "priority": 5}, {"id": 2, "priority": 10}]. - Output: A list of up to 3 task IDs, sorted by priority (highest first).
- Constraints:
- Task list length: 0 to 100.
- Priority: 1 to 100.
- IDs: unique integers, 1 to 1000.
def process_tasks(tasks):
# Your code here
passprint(process_tasks([
{"id": 1, "priority": 5},
{"id": 2, "priority": 10},
{"id": 3, "priority": 3}
])) # [2, 1, 3]
print(process_tasks([])) # []
print(process_tasks([
{"id": 1, "priority": 10},
{"id": 2, "priority": 10},
{"id": 3, "priority": 10},
{"id": 4, "priority": 5}
])) # [1, 2, 3]When handling data (like user records), duplicates can cause errors, like sending the same email twice. Deduplicating data is a common task in data processing or database management.
Write a function deduplicate_records that removes duplicate records from a list of dictionaries based on a specified key (e.g., email). Keep the first occurrence of each unique value and return the deduplicated list.
- Input: A list of dictionaries and a key string, e.g.,
[{"email": "[email protected]", "name": "A"}, {"email": "[email protected]", "name": "B"}], key="email". - Output: A list with duplicates removed based on the key.
- Constraints:
- List length: 0 to 100.
- Key exists in all records.
- Key values are strings.
def deduplicate_records(records, key):
# Your code here
passprint(deduplicate_records([
{"email": "[email protected]", "name": "A"},
{"email": "[email protected]", "name": "B"}
], "email")) # [{"email": "[email protected]", "name": "A"}]
print(deduplicate_records([], "email")) # []
print(deduplicate_records([
{"id": "1", "value": "x"},
{"id": "2", "value": "x"},
{"id": "3", "value": "y"}
], "value")) # [{"id": "1", "value": "x"}, {"id": "3", "value": "y"}]