This module supports R1, R4, and R6. All work here is formative. Pick Java or Python and stay with it.
Specification checklist
I can append to a list stored in a map instead of replacing it.
I can update a count with get-or-default plus one.
I can trace the map after each insertion.
Traced/worked example
Map<Character, List<String>> groups = new LinkedHashMap<>();
for (String word : List.of("apple", "ant", "banana")) {
char key = word.charAt(0);
groups.putIfAbsent(key, new ArrayList<>());
groups.get(key).add(word);
}
After apple: {a=[apple]}. After ant: {a=[apple, ant]}. After banana: {a=[apple, ant], b=[banana]}.
Replacing with groups.put(key, List.of(word)) would leave only ant under a.
Practice
Guided: Trace grouping by length for apple, ant, banana, bear, cat. After each word, write the map.
Independent: Implement groupByFirstLetter and groupByLength.
Java: Practice.java, PracticeTest.java
javac Practice.java PracticeTest.java && java PracticeTest
Python: practice.py
python3 practice.py
Python dict / setdefault matches Java Map / putIfAbsent.
Common mistakes
- Overwriting the list for a key instead of appending.
- Using a set when you need to keep every word, not uniqueness of keys.
- Assuming hash-map iteration is sorted.
Exit guidance
Complete the self-check and tests. Explain what one map entry represents after the five-word example.
Loading quiz...