There are two possible approach to this.
You can achieve that by modifying the presentation style to search each topic for presence or absence of code and adjust the filter.
Another way is to write a custom component, which will run after the main transformation. It can search for
absence of code/example and remove the filter. This is recommended so that the presentation style is untouched.
The tricky part may be handling languages like XML, which do not support the syntax filter.
If you have any further question on any of these, let me know.