Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dictionary for grouping anagrams.
DSA Python
anagrams = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list [] instead of a dictionary.
Using a set() which does not store key-value pairs.
✗ Incorrect
We use an empty dictionary {} to store groups of anagrams with keys as sorted strings.
2fill in blank
mediumComplete the code to get the sorted string key for each word.
DSA Python
key = ''.join(sorted([1]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dictionary name instead of the word.
Using an undefined variable.
✗ Incorrect
We sort the characters of the current word to create a key for grouping anagrams.
3fill in blank
hardFix the error in adding the word to the anagram group.
DSA Python
anagrams.setdefault(key, []).[1](word) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using add() which is for sets, not lists.
Using extend() which expects an iterable.
✗ Incorrect
We use append() to add the word to the list of anagrams for the key.
4fill in blank
hardFill both blanks to create the final list of grouped anagrams.
DSA Python
result = [[1] for [2] in anagrams.values()]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys instead of values.
Using variable names that do not match the group concept.
✗ Incorrect
We iterate over the values (groups) in the dictionary and collect each group into a list.
5fill in blank
hardFill all three blanks to complete the function that groups anagrams.
DSA Python
def group_anagrams([1]): anagrams = {} for [2] in [3]: key = ''.join(sorted(word)) anagrams.setdefault(key, []).append(word) return list(anagrams.values())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Not matching the parameter and loop variable names.
✗ Incorrect
The function takes a list 'words', loops over each 'word' in 'words', and groups anagrams.