Effective Environment After Layered ENV Instructions
Problem Statement
A Dockerfile sets environment variables across several ENV instructions; later ones override earlier keys. Compute the final environment.
Background
Each ENV instruction contributes one or more KEY=VALUE pairs. Applied top to bottom, a later assignment to the same key replaces the earlier value. The result is the merged mapping.
Your Task
def resolve_env(env_layers):
- env_layers: list of dicts, each a set of KEY: VALUE from one ENV line, in order.
- Return the final merged dict.
Input Format
- env_layers (list of dicts).
Output Format
- A dict.
Sample
print(resolve_env([{"A": "1", "B": "2"}, {"B": "9"}]))
Output:
{'A': '1', 'B': '9'}
Example
print(resolve_env([{"A": "1", "B": "2"}, {"B": "9"}])){'A': '1', 'B': '9'}- Start with an empty environment mapping to accumulate the final state.
- Process the first layer by merging its key-value pairs into the current mapping, resulting in .
- Process the second layer by updating the existing mapping; since key already exists, its value is overridden from to , while key remains unchanged.
- The merged mapping after all layers is applied is .
- The final output is {'A': '1', 'B': '9'}
Constraints
- Apply layers in order; later keys overwrite earlier ones.
- Keys absent in later layers keep their earlier value.
- Return the merged dict.