PIXELBANKv9.1.0
Menu

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:

Input:
print(reuse_ratio([[("a", 100)], [("a", 100)]]))
Output:
0.5
Reasoning:
  • Calculate the naive total by summing the sizes of all layers across every image, including duplicates: 100+100=200100 + 100 = 200.
  • 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: 100100.
  • Compute the reuse ratio using the formula 1−dedupnaive1 - \frac{\text{dedup}}{\text{naive}} to find the fraction of bytes saved by sharing layers: 1−100200=1−0.5=0.51 - \frac{100}{200} = 1 - 0.5 = 0.5.
  • Round the result to 4 decimal places as required by the problem specification: 0.5→0.50.5 \rightarrow 0.5.
  • 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.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.