27.5k views
5 votes
Write a python program for department library which has n books, write functions for following:

a) delete the duplicate entries to
b) display books in ascending order based on cost of books
c) count number of books with cost more than 500.
d) copy books in a new list which has cost less than 500

User Ppareja
by
8.3k points

2 Answers

3 votes

Final answer:

The response provides a Python program with a class called DepartmentLibrary that implements functions to delete duplicate entries, sort books by cost, count books with cost above 500, and copy books with cost below 500.

Step-by-step explanation:

Here is a Python program that implements the required functions for the department library with n books:

class DepartmentLibrary:
def __init__(self):
self.books = []

def add_book(self, book):
self.books.append(book)

def delete_duplicates(self):
self.books = list({book['isbn']: book for book in self.books}.values())

def display_books_ascending_cost(self):
return sorted(self.books, key=lambda book: book['cost'])

def count_books_above_500(self):
return len([book for book in self.books if book['cost'] > 500])

def copy_books_below_500(self):
return [book for book in self.books if book['cost'] < 500]

In this program, we define a class called DepartmentLibrary, with methods to handle different operations. To delete duplicate entries based on a unique 'isbn' identifier, we use a dictionary comprehension. The books are then sorted in ascending order based on the cost to display them, and counts of books with cost more than 500 as well as a copy of books with cost less than 500 are both created using list comprehensions.

User Rmunge
by
7.8k points
3 votes

Final answer:

The Python program includes functions to delete duplicate book entries, sort books by cost in ascending order, count books costing more than 500, and copy books that cost less than 500 into a new list for a department library.

Step-by-step explanation:

Python Program for a Department Library Management

Here is a Python program that manages books in a department library, based on the operations you've specified:

# Define a list of books where each book is a dictionary
# containing 'title' and 'cost'
books = [
{'title': 'Book1', 'cost': 450},
{'title': 'Book2', 'cost': 500},
... # Add more books as needed
]

# a) Function to delete duplicate entries
def delete_duplicates(books):
return list({book['title']: book for book in books}.values())

# b) Function to display books in ascending order based on cost
def display_books_sorted_cost(books):
return sorted(books, key=lambda book: book['cost'])

# c) Function to count books with cost more than 500
def count_expensive_books(books):
return sum(1 for book in books if book['cost'] > 500)

# d) Function to copy books with cost less than 500
def copy_cheaper_books(books):
return [book for book in books if book['cost'] < 500]

# Example usage
books = delete_duplicates(books)
print('Books sorted by cost:', display_books_sorted_cost(books))
print('Number of expensive books:', count_expensive_books(books))
cheap_books = copy_cheaper_books(books)
print('Cheap books:', cheap_books)

Make sure to replace the '...' in the books list with actual book entries. The functions provided perform the operations of deleting duplicates, sorting books by cost, counting books more expensive than 500, and copying books that are cheaper than 500 into a new list.

User Electrino
by
7.9k points