Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize an empty dictionary for counting characters.
DSA Python
freq = [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 string '' which cannot store counts.
Using a set() which does not store counts.
✗ Incorrect
We use an empty dictionary {} to store character counts.
2fill in blank
mediumComplete the code to loop through each character in the string 'text'.
DSA Python
for [1] in text: freq[char] = freq.get(char, 0) + 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one inside the loop.
Using 'word' which implies multiple characters.
Using 'item' which is vague.
✗ Incorrect
We use 'char' as the variable name to represent each character in the string.
3fill in blank
hardFix the error in the code to correctly update the frequency count.
DSA Python
freq[char] = freq.[1](char, 0) + 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' which is not a dictionary method.
Using 'set' which replaces the whole dictionary.
Using 'append' which is a list method.
✗ Incorrect
The 'get' method returns the current count or 0 if the character is not yet in the dictionary.
4fill in blank
hardFill both blanks to create a dictionary comprehension that counts characters only if they appear more than once.
DSA Python
freq = {char: text.count(char) for char in [1] if text.count(char) [2] 1} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using list(text) which includes duplicates.
Using '<' instead of '>' causing wrong filtering.
Using dict(text) which is invalid here.
✗ Incorrect
We use set(text) to avoid counting duplicates multiple times and '>' to filter characters appearing more than once.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that counts characters and includes only those with count at least 2.
DSA Python
freq = [1]: [2] for [3] in set(text) if text.count(char) >= 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing the opening curly brace.
Using wrong variable names.
Incorrect order of key and value.
✗ Incorrect
The dictionary comprehension syntax requires curly brace start '{char', value expression 'text.count(char)', and loop variable 'char'.