0%

Transforming and Combining Data · capstone

Capstone project: Project: Normalize, Rank, and Summarize Attempts

This chapter developed several ways to transform and summarize collections. The project now combines them to handle attempt data that arrives as unclean .

The program will move that data through a three-stage flow:

  1. Normalize values into one consistent shape.

  2. Rank attempts by score and completion time.

  3. Summarize the normalized collection.

Each stage receives the result of the stage before it and returns a new . The original raw records remain unchanged, so every step can be understood and checked separately.

Normalize

Raw records contain strings:

{
    "learner": "  mina ",
    "score": "8",
    "total": "10",
    "seconds": "75",
}

Clean a learner name

You already know that .strip() removes surrounding whitespace:

Try it

This prints mina jansen. The original string is unchanged.

Normalize each learner with strip() and convert numeric fields with int().

Rank

Use a key:

(-attempt["score"], attempt["seconds"])

Higher scores come first. Equal scores are resolved by lower completion time.

Summarize

The summary reports:

  • count: the number of attempts;

  • points: the sum of scores;

  • lowest and highest;

  • has_perfect: whether any score equals its total;

  • all_complete: whether every record has a non-negative completion time.

For an empty , points are zero, lowest and highest are None, and both results are False.

Put the operations in a safe order:

Drag the options into order, or use the arrow buttons.

  • Convert raw strings to normalized values.

  • Rank the normalized records.

  • Extract scores and Boolean checks.

  • Display the returned results.

  • Calculate the summary.

Keep stages independent

Hidden checks call each with unseen data. Do not combine all work into the visible top-level example. Small public functions let later programs reuse normalization, ranking, or summary separately.

This is the final -based processing flow before Chapter 4 introduces .

Task

Complete the three project .

normalize_attempts:

  • Return new that strip surrounding whitespace from learner names without changing letter case, and convert numeric fields to integers.

  • Do not modify the raw input.

rank_attempts:

  • Return a new sorted by descending score, then ascending seconds.

summarize_attempts:

  • Return the six documented fields.

  • Use sum, min, max, any, and all.

  • Build the score and collections with readable list comprehensions.

  • Use the list’s truthiness to handle an empty list with the documented values.

The provided program normalizes first, then ranks and summarizes.