Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created July 29, 2025 20:15
Show Gist options
  • Save decagondev/36d48ff63652f7c53242d65e775d9c29 to your computer and use it in GitHub Desktop.
Save decagondev/36d48ff63652f7c53242d65e775d9c29 to your computer and use it in GitHub Desktop.

Python Problem Sets for Learning Problem Solving and Software Development

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!

Problem Set 1: Log File Parser

Why This Matters

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.

Your Task

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 and Output

  • Input: A string log_entry (e.g., "[2025-07-29 09:06:00] INFO: User logged in").
  • Output: A dictionary with timestamp, level, and message, or None if invalid.
  • Constraints:
    • Valid levels: "INFO", "ERROR", "WARNING".
    • Timestamp format: "[YYYY-MM-DD HH:MM:SS]".
    • Input string length: 0 to 100 characters.

Starter Code

def parse_log_entry(log_entry):
    # Your code here
    pass

Test Cases

print(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"}

Problem Set 2: Configuration File Reader

Why This Matters

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.

Your Task

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 and Output

  • Input: A string config with 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 "=").

Starter Code

def read_config(config):
    # Your code here
    pass

Test Cases

print(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"}

Problem Set 3: Task Queue Processor

Why This Matters

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.

Your Task

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 and Output

  • 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.

Starter Code

def process_tasks(tasks):
    # Your code here
    pass

Test Cases

print(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]

Problem Set 4: Data Deduplication

Why This Matters

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.

Your Task

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 and Output

  • 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.

Starter Code

def deduplicate_records(records, key):
    # Your code here
    pass

Test Cases

print(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"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment