Word Analytics
Part of the Logic & Flow section of Coddy's Python journey — lesson 77 of 78.
Challenge
HardCreate a function named analyze_text that analyzes words in a text string.
Your function should:
- Count unique words (case-insensitive)
- Find all words that appear more than once
- Identify all palindrome words (words that read the same forwards and backwards, like "level")
Return a dictionary with three keys:
unique_count: the number of unique words (count also repeated words once each)repeated_words: a sorted list of words appearing more than oncepalindromes: a sorted list of palindrome words
Notes:
- Treat words as case-insensitive (e.g., "Hello" and "hello" are the same word)
- Remove any punctuation
(.,!?:;"())from words - Sort both the
repeated_wordsandpalindromeslists alphabetically
Example Input:
"Madam saw a racecar. Dad said hello hello to mom."
Expected Output:
{
'unique_count': 9,
'repeated_words': ['hello'],
'palindromes': ['a', 'dad', 'madam', 'mom', 'racecar']
}Try it yourself
def analyze_text(text):
# Your solution here
# 1. Split the text into words and normalize them
# (make lowercase and remove punctuation)
# 2. Count the occurrences of each word
# 3. Find the number of unique words
# 4. Identify repeated words (appearing more than once)
# 5. Find palindrome words
# 6. Return the results in a dictionary with sorted lists
passAll lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter