[Lab Evaluation] Library Catalog
Submit solution
Python
Points:
5 (partial)
Time limit:
0.5s
Memory limit:
12M
Author:
Problem type
Allowed languages
Library Catalog
You are working on a program to manage a library catalog. Implement a Python class called Library
that represents a library. The Library
class should have the following features:
An attribute
catalog
that is a dictionary representing the library catalog. The keys are book titles, and the values are the corresponding authors.Implement the
__init__
method to initialize the library with an empty catalog.Implement the
add_book
method that takes a book title and an author as arguments and adds the book to the catalog.Implement the
remove_book
method that takes a book title as an argument and removes the book from the catalog.Implement the
get_author
method that takes a book title as an argument and returns the author of that book.
Example Usage:
# Create a library
my_library = Library()
# Add books to the catalog
my_library.add_book("Thirukural", "Thiruvalluvar")
my_library.add_book("SS Nair", "Ramaguru Radhakrishnan")
# Print the catalog
print(my_library.catalog)
# Output: {'Thirukural': 'Thiruvalluvar', 'SS Nair': 'Ramaguru Radhakrishnan'}
# Get the author of a specific book
author = my_library.get_author("SS Nair")
print(author)
# Output: Ramaguru Radhakrishnan
# Remove a book from the catalog
my_library.remove_book("SS Nair")
# Print the updated catalog
print(my_library.catalog)
# Output: {'Thirukural': 'Thiruvalluvar'}
Testcases
Input 1
1
Thirukural
Thiruvalluvar
1
SS Nair
Ramaguru Radhakrishnan
4
5
Output 1
Book 'Thirukural' by Thiruvalluvar has been added to the catalog.
Book 'SS Nair' by Ramaguru Radhakrishnan has been added to the catalog.
Library Catalog:
1. Thirukural by Thiruvalluvar
2. SS Nair by Ramaguru Radhakrishnan
Exiting the Library Management System. Goodbye!
Input 2
1
Thirukural
Thiruvalluvar
1
SS Nair
Ramaguru Radhakrishnan
4
2
SS Nair
4
5
Output 2
Book 'Thirukural' by Thiruvalluvar has been added to the catalog.
Book 'SS Nair' by Ramaguru Radhakrishnan has been added to the catalog.
Library Catalog:
1. Thirukural by Thiruvalluvar
2. SS Nair by Ramaguru Radhakrishnan
Book 'SS Nair' has been removed from the catalog.
Library Catalog:
1. Thirukural by Thiruvalluvar
Exiting the Library Management System. Goodbye!
Input 3
1
Thirukural
Thiruvalluvar
1
SS Nair
Ramaguru Radhakrishnan
3
Thirukural
5
Output 3
Book 'Thirukural' by Thiruvalluvar has been added to the catalog.
Book 'SS Nair' by Ramaguru Radhakrishnan has been added to the catalog.
The author of 'Thirukural' is: Thiruvalluvar
Exiting the Library Management System. Goodbye!
Comments