Review a set of planned charts and explain what is wrong with each, the way an editor reviews a report.
What to build
A program that checks chart choices against the rules of good practice.
Requirements
- Each line holds a chart (
pie,bar,line,scatterorhistogram), a goal (compare,trend,distribution,relationshiporcomposition), the number of categories andyesornofor negative values. - Problems, in this order: a pie whose goal is not composition:
a pie only shows parts of a whole; a pie with more than 5 categories:too many slices for a pie (N); a pie with negative values:a pie cannot show negative values; a line whose goal is compare:a line suggests an order; use bars; a scatter whose goal is not relationship:a scatter plot shows relationships; a histogram whose goal is not distribution:a histogram shows a distribution; a bar chart with more than 12 categories:too many bars to read (N); show the top ones. - Print
I. CHART for GOAL: okorI. CHART for GOAL:followed by the problems joined by;, with I counting from 1.
Input
Line 1: n. Then n lines CHART GOAL CATEGORIES NEGATIVES.
Output
n lines.
Constraints
1 ≤ n ≤ 50.
Example
Input:
4
pie composition 4 no
pie composition 9 yes
line compare 6 no
bar compare 20 no
Output:
1. pie for composition: ok
2. pie for composition: too many slices for a pie (9); a pie cannot show negative values
3. line for compare: a line suggests an order; use bars
4. bar for compare: too many bars to read (20); show the top ones
Why it works
Each rule comes from how people read charts: angles of many slices cannot be compared, lines imply order, and every chart type answers one kind of question, so a mismatch is worth flagging before anyone reads the chart.
Examples
Example 1 · Input
4 pie composition 4 no pie composition 9 yes line compare 6 no bar compare 20 no
Expected output
1. pie for composition: ok 2. pie for composition: too many slices for a pie (9); a pie cannot show negative values 3. line for compare: a line suggests an order; use bars 4. bar for compare: too many bars to read (20); show the top ones
Example 2 · Input
2 scatter trend 0 no histogram distribution 0 no
Expected output
1. scatter for trend: a scatter plot shows relationships 2. histogram for distribution: ok
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Collect the problems for each chart in a list, then print them joined by "; " or "ok".
Hint 2
Check the problems in the order the requirements list them.
Solution
Show one correct solution (try it yourself first)
def problems(chart, goal, count, negatives):
found = []
if chart == "pie":
if goal != "composition":
found.append("a pie only shows parts of a whole")
if count > 5:
found.append(f"too many slices for a pie ({count})")
if negatives:
found.append("a pie cannot show negative values")
if chart == "line" and goal == "compare":
found.append("a line suggests an order; use bars")
if chart == "scatter" and goal != "relationship":
found.append("a scatter plot shows relationships")
if chart == "histogram" and goal != "distribution":
found.append("a histogram shows a distribution")
if chart == "bar" and count > 12:
found.append(f"too many bars to read ({count}); show the top ones")
return found
n = int(input())
for i in range(1, n + 1):
chart, goal, count, negatives = input().split()
found = problems(chart, goal, int(count), negatives == "yes")
print(f"{i}. {chart} for {goal}: {'; '.join(found) if found else 'ok'}")