Skip to main content
ThePythonBook/Assessment

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.

Progress
900 XP0/10
#1Simple Calculator Parser
Write Code

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.

  • The expression format is always: "number operator number" (space-separated).
  • Return an int when the result is a whole number, otherwise return a float.
  • For division by zero, return the string "Error: division by zero".
  • Loading editor...
    #2Mini Task Manager
    Write Code

    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 results
  • Return a list of lists, where each inner list is the snapshot produced by a "list" command.

    Loading editor...
    #3Fix the Text Summarizer
    Fix the Bug

    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.

    Loading editor...
    #4Caesar Cipher Encoder/Decoder
    Write Code

    Write a function caesar(text, shift, mode="encode") that applies a Caesar cipher.

  • When mode is "encode", shift each letter forward by shift positions.
  • When mode is "decode", shift each letter backward by shift positions.
  • Preserve case (uppercase stays uppercase, lowercase stays lowercase).
  • Non-letter characters are left unchanged.
  • The shift wraps around the alphabet (e.g., shifting 'z' by 1 gives 'a').
  • Loading editor...
    #5Predict the Data Pipeline Output
    Predict Output

    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))
    Loading editor...
    #6Refactor the URL Shortener
    Refactor

    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.
  • Use the same hash-based code generation: hashlib.md5(url.encode()).hexdigest()[:6].
  • Loading editor...
    #7Fix the Word Frequency Counter
    Fix the Bug

    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.

    Loading editor...
    #8Mini Templating Engine
    Write Code

    Write a function render(template, context) that replaces placeholders in a template string with values from a context dictionary.

  • Placeholders look like {{key}} (double curly braces, no spaces inside).
  • If a key is missing from the context, leave the placeholder as-is.
  • Return the rendered string.
  • Loading editor...
    #9Predict the Decorator Pipeline Output
    Predict Output

    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))
    Loading editor...
    #10Refactor the CSV Data Processor
    Refactor

    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.

    Loading editor...