Day 13 - Python

Title: Book Catalog Operations

Problem Statement:

You are given a list of dictionaries representing books written by two famous Indian authors, Kalki and Sandilyan. Each dictionary contains information about a book, including its title, author, and publication year. Your task is to implement various operations on this book catalog using Python.

Dictionary:

books = [

    {"title": "Ponniyin Selvan", "author": "Kalki", "year": 1950},

    {"title": "Sivagamiyin Sabatham", "author": "Kalki", "year": 1944},

    {"title": "Parthiban Kanavu", "author": "Kalki", "year": 1941},

    {"title": "Solaimalai Ilavarasi", "author": "Kalki", "year": 1947},

    {"title": "Alai Osai", "author": "Kalki", "year": 1948},

    {"title": "Thyaga Bhoomi", "author": "Kalki", "year": 1938},

    {"title": "Magudapathi", "author": "Kalki", "year": 1942},

    {"title": "Kadal Pura", "author": "Sandilyan", "year": 1967},

    {"title": "Yavana Rani", "author": "Sandilyan", "year": 1977}, 

    {"title": "Raja Perigai", "author": "Sandilyan", "year": 1952}, 

    {"title": "Mannan Magal", "author": "Sandilyan", "year": 1963}, 

    {"title": "Rajamuthirai", "author": "Sandilyan", "year": 1962},

    {"title": "Chandramathi", "author": "Sandilyan", "year": 1971},

    {"title": "Indirakumari", "author": "Sandilyan", "year": 1956}

]

Operations to Implement:

  1. Filter books written by a specific author.
  2. Filter books published after a certain year.
  3. Extract titles of all books.
  4. Sort books based on their publication year.
  5. Count the number of books written by a specific author.
  6. Find the earliest published book.
  7. Find the latest published book.
  8. Calculate the average publication year of the books.
  9. Check if a book with a given title exists.
  10. Find the book with the longest title.

Output Format:

For each operation, print the result as specified below:

For operations 1, 2, 3, 4, 6, 7, and 9, print the corresponding output in a separate line.

For operations 5, 8, and 10, print the result rounded to two decimal places.

Explanation:

For operation 1, books written by the author "Kalki" are filtered.

For operation 2, books published after the year 1950 are filtered.

For operation 3, titles of all books are extracted.

For operation 4, books are sorted based on their publication year.

For operation 5, the number of books written by the author "Sandilyan" is counted.

For operation 6, the earliest published book is found.

For operation 7, the latest published book is found.

For operation 8, the average publication year of the books is calculated.

For operation 9, the existence of the book "Yavana Rani" is checked.

For operation 10, the book with the longest title is found.


SOLUTION:

Query 1:

def filter_by_author(author, books):

    return [book['title'] for book in books if book['author'] == author]

print('\n'.join(filter_by_author("Kalki", books)))

Output 1:

Ponniyin Selvan

Sivagamiyin Sabatham

Parthiban Kanavu

Solaimalai Ilavarasi

Alai Osai

Thyaga Bhoomi

Magudapathi


Query 2:

def filter_by_year(year, books):

    return [book['title'] for book in books if book['year'] > year]

print('\n'.join(filter_by_year(1950, books)))

Output 2:

Kadal Pura

Yavana Rani

Raja Perigai

Mannan Magal

Rajamuthirai

Chandramathi

Indirakumari


Query 3:

def extract_titles(books):

    return [book['title'] for book in books]

print('\n'.join(extract_titles(books)))

Output 3:

Ponniyin Selvan

Sivagamiyin Sabatham

Parthiban Kanavu

Solaimalai Ilavarasi

Alai Osai

Thyaga Bhoomi

Magudapathi

Kadal Pura

Yavana Rani

Raja Perigai

Mannan Magal

Rajamuthirai

Chandramathi

Indirakumari


Query 4:

def sort_by_year(books):

    return [(book['title'], book['year']) for book in sorted(books, key=lambda x: x['year'])]

sorted_books = sort_by_year(books)

print('\n'.join([f"{title} - {year}" for title, year in sorted_books]))

Output 4:

Thyaga Bhoomi - 1938

Parthiban Kanavu - 1941

Magudapathi - 1942

Sivagamiyin Sabatham - 1944

Solaimalai Ilavarasi - 1947

Alai Osai - 1948

Ponniyin Selvan - 1950

Raja Perigai - 1952

Indirakumari - 1956

Rajamuthirai - 1962

Mannan Magal - 1963

Kadal Pura - 1967

Chandramathi - 1971

Yavana Rani - 1977


Query 5:

def count_books_by_author(author, books):

    return round(len([book for book in books if book['author'] == author]), 2)

print(count_books_by_author("Sandilyan", books))

Output 5:

7


Query 6:

def earliest_book(books):

    return min(books, key=lambda x: x['year'])['year']

print(earliest_book(books))

Output 6:

1938


Query 7:

def latest_book(books):

    return max(books, key=lambda x: x['year'])['year']

print(latest_book(books))

Output 7:

1977


Query 8:

def average_year(books):

    return round(sum(book['year'] for book in books) / len(books), 2)

print(average_year(books))

Output 8:

1954.14


Query 9:

def book_exists(title, books):

    return any(book['title'] == title for book in books)

print(book_exists("Yavana Rani", books))

Output 9:

True


Query 10:

def longest_title_book(books):

    return max(books, key=lambda x: len(x['title']))['title']

print(longest_title_book(books))

Output 10:

Sivagamiyin Sabadham


Hope everything is clear in this program using List Comprehension in Dictionaries! Doubts are welcome in the comment section!

Enjoy Coding with our wonderful Novels! Read 📖 and Code 👨‍💻 with a cup of ☕! :)

Comments