Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Implement a Book and Author CompositionE02. Create a ShoppingCart with Product ItemsE03. Model a Playlist with Track ComponentsE

Problem No 1

Implement a Book and Author Composition

Easy

8 minute session

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.

Code editor
Loading editor…

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.

09:16 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.