Write a Python function that takes a list of dictionaries representing articles and their metadata (title, author, tags, and word count) and returns a summary of the articles. The summary should include:
-
The total number of articles.
-
A list of all unique tags across the articles.
-
The average word count of the articles.
Each dictionary in the list will have the following structure:
python
Copy code
{ ‘title’: ‘Article Title’, ‘author’: ‘Author Name’, ‘tags’: [‘tag1’, ‘tag2’], ‘word_count’: 1000 }
Function Signature:
python
Copy code
def article_summary(articles: list[dict]) -> dict: pass
Example Input:
python
Copy code
articles = [ {‘title’: ‘AI in 2024’, ‘author’: ‘John Doe’, ‘tags’: [‘AI’, ‘ML’], ‘word_count’: 1200}, {‘title’: ‘Python Best Practices’, ‘author’: ‘Jane Smith’, ‘tags’: [‘Python’, ‘Coding’], ‘word_count’: 850}, {‘title’: ‘Content Marketing’, ‘author’: ‘Alice Brown’, ‘tags’: [‘Marketing’, ‘Content’], ‘word_count’: 950} ]
Expected Output:
python
Copy code
{ ‘total_articles’: 3, ‘unique_tags’: [‘AI’, ‘ML’, ‘Python’, ‘Coding’, ‘Marketing’, ‘Content’], ‘average_word_count’: 1000.0 }
I tried implementing the article_summary
function to process a list of article dictionaries. I expected the function to return the total number of articles, a list of unique tags, and the average word count across all articles. However, when I ran my code, the unique tags list was incorrect because some tags were repeated, and the average word count calculation didn’t work as expected. I was expecting a summary with correct unique tags and the proper average word count, but the output had duplicates and an incorrect average
You need to sign in to view this answers