Count words while several threads share a result.

Prototype: implement SafeCounter() with increment() and read-only value. It starts at 0; every increment adds exactly 1 and returns None. Protect both updates and reads with a threading.Lock so concurrent increments are safe.

Final: count_words(chunks) accepts a list of iterables of strings. Start one real threading.Thread per chunk, consume each chunk in that worker, and return a dictionary of total word counts only after all threads are joined. Matching is case-sensitive; empty strings and duplicates are counted. Empty chunks returns {}. Do not modify input lists. Protect shared count updates with one lock. Example: [["a", "b"], ["a"]] -> {"a": 2, "b": 1}. Chunk iteration is assumed not to raise. Scheduling and dictionary iteration order are unspecified. Native Python is required; no performance threshold is tested.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
Run this exercise with local Python.

This exercise starts native threads or processes, which browser Python does not support. Download your code and tests, then run the command below.

python3 main.py