THINK FIRST·CODE LATER

← All labs

KPI cards with context and sparklines

Problem

Input: a header line kpi NAME target T higher_is_better|lower_is_better, then a CSV with date and the KPI column (daily values).

Using the last 7 days and the 7 days before them, print:

trips: last 7 days mean 1333
  vs previous 7 days: +8.7% (better)
  target 1500: -11.1% -> off track
  trend: ▅▆▅▆▇▁▁▆▇▆▇█▂▂  (min 850, max 1600)

The verdict is better or worse depending on the direction (for lower_is_better a decrease is better), or no change if the change is exactly 0; "on track" means mean ≥ target (higher is better) or ≤ target (lower is better). The sparkline maps each of the last 14 values to one of the 8 characters ▁▂▃▄▅▆▇█ with index ⌊(v − min)/(max − min) × 7 + 0.5⌋ (all ▁ if max = min). Means are printed with 0 decimals; percentages with :+.1%.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v9-1-data-1.txt — first 14 lines of 16

kpi trips target 1500 higher_is_better
date,trips
2026-03-01,1300
2026-03-02,1350
2026-03-03,1280
2026-03-04,1400
2026-03-05,1500
2026-03-06,900
2026-03-07,850
2026-03-08,1420
2026-03-09,1480
2026-03-10,1390
2026-03-11,1510
2026-03-12,1600
What your program must print for v9-1-data-1.txt
trips: last 7 days mean 1333
  vs previous 7 days: +8.7% (better)
  target 1500: -11.1% -> off track
  trend: ▅▆▅▆▇▁▁▆▇▆▇█▂▂  (min 850, max 1600)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v9-1-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v9-1-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v9-1-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.