Layer Reuse Ratio Across an Image Set
Problem Statement
Quantify how much a set of images benefits from shared layers by comparing the naive total (sum over all image layers) to the deduplicated total.
Background
The naive size sums every layer of every image (double-counting shared layers). The dedup size counts each digest once. The reuse ratio is 1 - dedup/naive — the fraction of bytes saved by sharing. With a naive total of 0, the ratio is 0.0.
Your Task
def reuse_ratio(images):
- images: list of images, each a list of (digest, size).
- Return the reuse ratio, rounded to 4 decimals.
Input Format
- images (list of lists of (str, int)).
Output Format
- A float rounded to 4 decimals.
Sample
print(reuse_ratio([[("a", 100)], [("a", 100)]]))
Output:
0.5
Example
print(reuse_ratio([[("a", 100)], [("a", 100)]]))0.5
- Calculate the naive total by summing the sizes of all layers across every image, including duplicates: .
- Determine the dedup total by summing the size of each unique digest only once; since digest "a" appears in both images, it is counted a single time: .
- Compute the reuse ratio using the formula to find the fraction of bytes saved by sharing layers: .
- Round the result to 4 decimal places as required by the problem specification: .
- The final output is 0.5
Constraints
- naive = sum of all layer sizes (with repeats); dedup = sum over unique digests.
- ratio = 1 - dedup/naive; naive==0 -> 0.0.
- Round to 4 decimals.