THINK FIRST·CODE LATER

← All labs

Pick the colour scheme from the attribute type

Problem

Input lines: name type=KIND [n=COUNT] [mid=VALUE] where KIND is categorical, sequential, diverging or cyclic.

Rules: categorical with n ≤ 10 → scheme tableau10 (type N), reason 6 categories: distinct hues; categorical with n > 10 → print station: no colour encoding (80 categories: too many hues -> use position, facets or highlight a few); sequential → viridis (type Q), reason ordered low->high: perceptually uniform luminance ramp; diverging → blueorange with domainMid = mid (default 0), reason two directions around 0; cyclic → sinebow, reason wraps around: cyclic hue scheme.

Build the colour encoding with Altair (alt.Color(f"{name}:{type}", scale=alt.Scale(...)) on a mark_rect chart) and print the scale dictionary from the chart specification: pm25: sequential -> {'scheme': 'viridis'} (ordered low->high: perceptually uniform luminance ramp).

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.

v6-3-data-1.txt

district type=categorical n=6
station type=categorical n=80
pm25 type=sequential
change type=diverging mid=0
temp_anomaly type=diverging mid=0.5
hour type=cyclic
What your program must print for v6-3-data-1.txt
district: categorical -> {'scheme': 'tableau10'} (6 categories: distinct hues)
station: no colour encoding (80 categories: too many hues -> use position, facets or highlight a few)
pm25: sequential -> {'scheme': 'viridis'} (ordered low->high: perceptually uniform luminance ramp)
change: diverging -> {'domainMid': 0.0, 'scheme': 'blueorange'} (two directions around 0)
temp_anomaly: diverging -> {'domainMid': 0.5, 'scheme': 'blueorange'} (two directions around 0.5)
hour: cyclic -> {'scheme': 'sinebow'} (wraps around: cyclic hue scheme)

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 < v6-3-data-1.txt

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

Get-Content v6-3-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("v6-3-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.