Practice/Google/String Template Substitution with Variable Replacement
CodingMust
Build a template expansion engine that processes strings containing placeholders and replaces them with values from a provided dictionary. Placeholders are enclosed in curly braces like {variable}. The challenge is that replacement values themselves may contain additional placeholders, requiring recursive or iterative resolution until no more substitutions can be made.
Your function should continue expanding placeholders until either all placeholders are resolved or no further progress can be made (when placeholders reference missing keys).
Example 1:
Input: template = "User {name} has {role} access" mapping = {"name": "John", "role": "admin"} Output: "User John has admin access" Explanation: Direct substitution of two placeholders with their values.
Example 2:
Input: template = "Message: {msg}" mapping = {"msg": "{prefix}: {content}", "prefix": "INFO", "content": "System ready"} Output: "Message: INFO: System ready" Explanation: The value for 'msg' contains two more placeholders that need expansion.
Example 3:
Input: template = "{a} and {b}" mapping = {"a": "{b}", "b": "{a}"} Output: "{a} and {b}" Explanation: Circular reference detected - substitutions cannot progress, so original placeholders remain.