Design Patterns: Behavioral · Coding problem
Report Exporters From One Template Method
A free coding problem from the Template Method Pattern skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
ReportExporter.export(rows) is a template method: it's final, it fixes the order of the steps (header, then each included row, then an optional footer), and it calls methods that subclasses fill in. Two steps are required (header, formatRow); two are hooks with defaults (includeRow includes everything, footer adds nothing).
In Python: the steps are header, format_row, include_row and footer, and export is marked @typing.final - a type checker rejects an override, though the interpreter doesn't stop one.
Implement the three exporters:
CsvExporter- header linename,score; each rowann,90.MarkdownExporter- header lines| name | score |and|---|---|; each row| ann | 90 |; footerrows: <N>.PassingCsvExporter- extendsCsvExporterand overrides only the hooks: include rows with score >= 50, footerpassing: <N>.
Input: the format (csv, markdown or passing), a row count, then <name> <score> lines.
Output: the exported report.
Example
Input:
markdown
2
ann 90
bob 45
Output:
| name | score |
|---|---|
| ann | 90 |
| bob | 45 |
rows: 2
Constraints
- At most 1000 rows; scores are between 0 and 100.
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Template Method Pattern
Solve it Report Exporters From One Template Method free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime