MediumPro challengePython

Observer

PythonDesign PatternsBehavioral

Observer lets a Subject broadcast events to any number of
Observers that have subscribed, without knowing their concrete types.

Implement Subject.attach(observer), Subject.detach(observer) and
Subject.notify(event) (calls .update(event) on every attached observer).

solve(operations) drives one shared Subject through 'attach' /
'detach' / 'notify' ops (each carrying an observer id) and returns
the combined [[id, event], ...] delivery log.

Sample tests

Test #1Basic attach + notify
Input: [[{"id":"s1","op":"attach"},{"op":"notify","event":"hello"}]]
Output: [["s1","hello"]]
Test #2Two observers both receive the event
Input: [[{"id":"s1","op":"attach"},{"id":"s2","op":"attach"},{"op":"notify","event":"x"}]]
Output: [["s1","x"],["s2","x"]]
Test #3Detach before notify receives nothing
Input: [[{"id":"s1","op":"attach"},{"id":"s1","op":"detach"},{"op":"notify","event":"nothing"}]]
Output: []
Test #4Notify with no subscribers
Input: [[{"op":"notify","event":"no-subs"}]]
Output: []