THINK FIRST·CODE LATER

← All labs

A slice-and-dice treemap

Problem

Input: a line canvas W H, then a CSV district,building,kwh (rows grouped by district, in display order).

Compute the slice-and-dice layout: districts split the width left to right in input order (width ∝ district total), and each district's rectangle is split top to bottom by its buildings (height ∝ building kWh). Print per district Lucheng: x=0 y=0 w=50 h=60 (50%) and per building Library: x=0 y=0 w=50 h=36 aspect 1.4 (aspect = max(w/h, h/w), 1 decimal; coordinates with at most 1 decimal, without trailing .0). Then print worst aspect ratio: Airport 12.0 (squarified layouts keep rectangles closer to 1) and total 1000 kWh on a 100 x 60 canvas.

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.

v10-4-data-1.txt

canvas 100 60
district,building,kwh
Lucheng,Library,300
Lucheng,City Hall,200
Ouhai,Gym,150
Ouhai,School,250
Ouhai,Pool,50
Longwan,Airport,50
What your program must print for v10-4-data-1.txt
Lucheng: x=0 y=0 w=50 h=60 (50%)
  Library: x=0 y=0 w=50 h=36 aspect 1.4
  City Hall: x=0 y=36 w=50 h=24 aspect 2.1
Ouhai: x=50 y=0 w=45 h=60 (45%)
  Gym: x=50 y=0 w=45 h=20 aspect 2.2
  School: x=50 y=20 w=45 h=33.3 aspect 1.3
  Pool: x=50 y=53.3 w=45 h=6.7 aspect 6.8
Longwan: x=95 y=0 w=5 h=60 (5%)
  Airport: x=95 y=0 w=5 h=60 aspect 12.0
worst aspect ratio: Airport 12.0 (squarified layouts keep rectangles closer to 1)
total 1000 kWh on a 100 x 60 canvas

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 < v10-4-data-1.txt

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

Get-Content v10-4-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("v10-4-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.