Problem No 1
Implement a Book and Author Composition
Easy≈ 8 minute session
Lesson guide
What this Python exercise practices
Implement a Book and Author Composition is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
Summary
Model books composed of authors and implement methods to manage and query author relationships.
Problem statement
You will create two classes: Author and Book. An Author stores basic information (name, birth year, optional bio). A Book is composed of one or more Authors and stores metadata (title, publication year, pages). Implement methods to manage the composition and to query relationships between a book and its authors. Required features: - Author: initialize with name, optional birth_year and bio. Implement readable string representation and equality comparison (authors are equal if name and birth_year match). - Book: initialize with title, optional list of Author objects, publication year, and pages. - Book.add_author(author): add an Author instance, avoiding duplicates. - Book.remove_author(name_or_author): remove an author by name (string) or by Author instance; if not present, do nothing. - Book.author_names(): return list of author names in order. - Book.co_authors(author_name): given the name of one author, return a list of the other authors' names (order preserved). If the author isn't on the book, return an empty list. - Book.is_by_author(author_name): returns True if the author is one of the book's authors. Design the classes to be easy to use in composition: a Book contains Author objects (not just strings). Keep behavior predictable for duplicate additions and removals.
Task
Use composition to model Book and Author classes, implement methods to add/remove authors, query co-authors and check authorship.
Examples
Single-author book
Input
Book('Deep Learning', [Author('Ian Goodfellow', 1985)], 2016, 800).author_names()
Output
['Ian Goodfellow']
Explanation
The book is created with a single Author; author_names returns a list with that author's name.
Co-authors query
Input
Book('Collab', [Author('Alice', 1975), Author('Bob', 1980)], 2020, 200).co_authors('Alice')
Output
['Bob']
Explanation
co_authors returns the names of all other authors besides the given author.
Input format
You will construct Author and Book objects and call methods on the Book instance. Each test is a single Python expression that returns a value.
Output format
Return values depend on the method: lists of strings for author lists, booleans for checks. The test harness compares str(value).
Constraints
- Do not use any external libraries. - Treat authors as equal when both name and birth_year match. - Methods should not print; they should return values. - Preserve author ordering unless an author is removed. - Avoid duplicate authors in a Book.
Samples
Sample input 0
b = Book('Solo Work', [Author('Solo', 1990)], 2010, 150) b.is_by_author('Solo')
Sample output 0
True
Explanation 0
The book has one author named 'Solo'; is_by_author returns True.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.