Template Variable Substitution
Implement a simple prompt template engine.
Given a template string with {{variable}} placeholders and a set of key-value pairs, substitute all placeholders with their values.
Input:
- Line 1: The template string
- Line 2: N (number of variables)
- Next N lines: key value (space-separated, value may contain spaces)
Output: The filled template string.
If a placeholder has no matching key, leave it as-is.
Example:
Hello {{name}}, you are {{role}}.
2
name Alice
role engineerHello Alice, you are engineer.
- The template string
Hello {{name}}, you are {{role}}.is given with two placeholders:{{name}}and{{role}}. - A set of key-value pairs is provided:
name=Aliceandrole=engineer. - The template engine substitutes the placeholders with their corresponding values, resulting in
Hello Alice, you are engineer.. - Since both
{{name}}and{{role}}have matching keys, they are replaced; if a placeholder had no match, it would remain unchanged.
Constraints:
- Placeholders use double curly braces: {{key}}
- Keys are alphanumeric (no spaces)
- Values may contain spaces
- Unmatched placeholders remain unchanged
More from LLM 3: Applications & Evaluation
Background Knowledge
The problem of template variable substitution falls under the category of string processing and template engines. A template engine is a system that replaces placeholders in a template with actual values. This concept is widely used in web development, where templates are used to separate presentation logic from application logic. In the context of Prompt Engineering & Parsing, template engines can be used to generate human-like text based on a set of input parameters.
To solve this problem, you need to understand the basics of string manipulation and dictionary data structures. String manipulation involves operations such as searching, replacing, and splitting strings. Dictionaries, on the other hand, are data structures that store key-value pairs, allowing for efficient lookups and insertions. In this problem, you will use a dictionary to store the key-value pairs and then use this dictionary to replace the placeholders in the template string.
The problem also requires an understanding of parsing, which is the process of analyzing a string of symbols to determine its structure and meaning. In this case, you need to parse the template string to identify the placeholders and then replace them with the corresponding values. This involves using techniques such as regular expressions or string splitting to extract the placeholders and then using a dictionary to look up the replacement values.
Algorithm/Approach
The general approach to solving this problem involves the following steps:
- Parse the template string to identify the placeholders
- Create a dictionary to store the key-value pairs
- Iterate over the placeholders and replace each one with its corresponding value from the dictionary
- Return the filled template string
This approach can be implemented using a variety of algorithms, including recursive descent parsing or iterative parsing. The choice of algorithm will depend on the specific requirements of the problem and the desired level of complexity.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Read the template string and store it in a variable
- Read the number of variables (N) and store it in a variable
- Create an empty dictionary to store the key-value pairs
- Iterate over the next N lines, reading each key-value pair and storing it in the dictionary
- Parse the template string to identify the placeholders
- Iterate over the placeholders, replacing each one with its corresponding value from the dictionary
- Return the filled template string
Some possible ways to parse the template string include:
- Using regular expressions to match the placeholder pattern ({{variable}})
- Splitting the string into substrings based on the placeholder delimiters ({{ and }})
Common Pitfalls
Some common pitfalls to watch out for when implementing this solution include:
- Failing to handle cases where a placeholder has no matching key
- Failing to handle cases where a key has multiple values
- Using an inefficient algorithm for parsing the template string
- Failing to handle edge cases, such as an empty template string or an empty dictionary
Time & Space Complexity
The expected time complexity for this solution is O(n + m), where n is the number of variables and m is the length of the template string. The space complexity is O(n), where n is the number of variables. This is because we need to store the key-value pairs in a dictionary, which requires O(n) space. The parsing and replacement operations require O(m) time, where m is the length of the template string.