Count hashtags in a social media post
Your task
Your analytics page shows which hashtags people use in their posts. Each post arrives as raw text.
Write count_hashtags(post) that returns how many times each hashtag appears in post.
Input and output:
post: a string of up to 10,000 characters.- Returns a dictionary that maps each tag, in lowercase and without its
#, to the number of times it appears. Key order does not matter.
Rules:
- A hashtag is a
#followed by one or more tag characters: letters, digits and combining marks. - Letters come from any alphabet, so
é,жand日can be part of a tag. - Digits are decimal digits in any script. Other number signs, such as
²and½, are not digits. - Combining marks are characters that attach to the one before them, such as an accent typed separately or a Hindi vowel sign.
- The tag ends just before the first character that is not a tag character. Spaces, punctuation,
_,-and emoji all end it. - A
#with no tag character right after it is not a hashtag. - A
#starts a hashtag wherever it appears: in the middle of a word, or right after another tag. - Tags that differ only in case are the same tag. Apart from case, compare tags exactly as typed.
- A post with no hashtags, including an empty post, returns an empty dictionary.
Examples
count_hashtags(post: str) -> dict[str,int]Example 1
Input
count_hashtags("Loving the new build #DevLife #devlife and #Rust!")Output
{"devlife": 2, "rust": 1}Example 2
Input
count_hashtags("#DevLife rocks #devlife")Output
{"devlife": 2}Example 3
Input
count_hashtags("Morning #Café, then #café_au_lait and #日本")Output
{"café": 2, "日本": 1}