0
0
DSA Pythonprogramming~10 mins

Group Anagrams Using Hash Map in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the dictionary for grouping anagrams.

DSA Python
anagrams = [1]
Drag options to blanks, or click blank then click option'
A{}
B[]
C()
Dset()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list [] instead of a dictionary.
Using a set() which does not store key-value pairs.
2fill in blank
medium

Complete 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'
Aword
Bkey
Canagrams
Dgroup
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dictionary name instead of the word.
Using an undefined variable.
3fill in blank
hard

Fix 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'
Aextend
Badd
Cinsert
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using add() which is for sets, not lists.
Using extend() which expects an iterable.
4fill in blank
hard

Fill 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'
Agroup
Bkey
Cgroups
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys instead of values.
Using variable names that do not match the group concept.
5fill in blank
hard

Fill 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'
Awords
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Not matching the parameter and loop variable names.