Question
Write a function that counts how often the given hashtags are used in all posts. The posts are given as a list of dictionaries ,
Write a function that counts how often the given hashtags are used in all posts. The posts are given as a list of dictionaries, where each dictionary has a text field and a list of hashtags used (see below doctests). The hashtags parameter is a list of strings representing hashtags to count. Return the sum of all counts of given hashtags.
You MUST solve this question with list comprehension. Your implementation should only return an expression. In other words, without the 79-character line length limit, your implementation should fit in exactly one line. No explicit loops (for loop or while loop) are allowed. Do NOT add any inline comments.
Doctest Explanations:
-
No hashtags to search in the inputs, therefore, we return 0
-
Elvy only appears once in the posts array, and we return 1
-
DSC20 appears twice in both first and second posts, and Debugging appears once in the second post; therefore, we return 3
-
Programming appears once, and Java appears once, thus we return 2
def count_hashtags(hashtags, posts):
"""
>>> count_hashtags([], [{'hashtags': ['quote'], \
'text': 'Smart person solves bugs, and wise person avoids bugs'}])
0
>>> count_hashtags(['Elvy'], \
[{'hashtags': ['Elvy'], 'text': 'Elvy is the queen in piazza.'}, {'hashtags': ['random'], 'text': 'this is such a random post.'}])
1
>>> count_hashtags(['DSC20', 'Debugging', 'Quiz'], \
[{'hashtags': ['school work', 'DSC20'], 'text': 'DSC20 is so much fun!'}, {'hashtags': ['Debugging', 'DSC20'], 'text': 'In order to debug, you have to be twice as smart as the person who writes the code!'}])
3
>>> count_hashtags(['Programming', 'Java'], \
[{'hashtags': ['Programming', 'Java'], 'text': 'javascript to java is carpet to car'}, {'hashtags': [], 'text': 'javascript to java is carpet to car'}])
2
"""
# YOUR CODE GOES HERE #
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started