Practical Projects Assessment
Put your Python skills to the test with real-world mini-projects. These exercises combine data processing, text manipulation, algorithm design, and structured problem-solving into practical coding challenges.
Write a function calculate(expression) that takes a string containing a simple arithmetic expression with two operands and one operator (+, -, *, /) and returns the numeric result.
"number operator number" (space-separated).int when the result is a whole number, otherwise return a float."Error: division by zero".Write a function task_manager(commands) that takes a list of command strings and simulates a task list.
Supported commands:
"add <task>" -- adds the task to the list"done <index>" -- marks the task at the 0-based index as done (prefix with "[DONE] ")"list" -- appends the current task list snapshot to the resultsReturn a list of lists, where each inner list is the snapshot produced by a "list" command.
This function is supposed to summarize text by returning the first n words followed by an ellipsis ("..."). If the text has n or fewer words it should return the text unchanged.
There are two bugs. Find and fix them.
Write a function caesar(text, shift, mode="encode") that applies a Caesar cipher.
mode is "encode", shift each letter forward by shift positions.mode is "decode", shift each letter backward by shift positions.Read the code below carefully and predict its exact printed output.
def pipeline(data, *transforms):
for fn in transforms:
data = fn(data)
return data
nums = [3, 1, 4, 1, 5, 9, 2, 6]
result = pipeline(
nums,
lambda xs: [x for x in xs if x > 2],
lambda xs: sorted(xs, reverse=True),
lambda xs: xs[:4]
)
print(result)
print(pipeline("hello world", str.upper, str.split))The code below implements a basic URL shortener but it is repetitive and hard to maintain. Refactor it into a class called URLShortener with methods shorten(url) and resolve(code).
Requirements:
shorten(url) returns a short code. If the URL was already shortened, return the same code.resolve(code) returns the original URL, or None if the code is unknown.hashlib.md5(url.encode()).hexdigest()[:6].This function should return a dictionary mapping each word to its frequency, ignoring case and stripping punctuation. It has two bugs. Find and fix them.
Write a function render(template, context) that replaces placeholders in a template string with values from a context dictionary.
{{key}} (double curly braces, no spaces inside).Read the code below carefully and predict its exact printed output.
def logger(func):
def wrapper(*args):
print(f"calling {func.__name__}")
result = func(*args)
print(f"result: {result}")
return result
return wrapper
def validate(func):
def wrapper(*args):
for a in args:
if not isinstance(a, (int, float)):
print("invalid input")
return None
return func(*args)
return wrapper
@logger
@validate
def add(a, b):
return a + b
print(add(2, 3))
print(add("x", 1))The code below processes CSV-like data (list of comma-separated strings) but is repetitive and uses too many intermediate variables. Refactor it into a clean function process_csv(rows) that:
1. Splits each row by commas
2. Strips whitespace from each field
3. Filters out rows where the first field is empty
4. Returns a list of lists (each inner list is the cleaned fields for one row)
Keep the same behavior but make it concise and Pythonic.