Cache hit ratio in permille
A proxy dashboard reports cache efficiency as permille — parts per thousand — so operators can spot a 2 permille dip that a percentage would round away.
- Return floor(hits × 1000 / requests).
- When requests is zero or negative, return 0.
cache_hit_permille(hits: int, requests: int) → int
Where you start
def cache_hit_permille(hits: int, requests: int) -> int:
Worked examples
| Call | Result |
|---|---|
cache_hit_permille(500, 1000) | 500 |
cache_hit_permille(1, 3) | 333 |
cache_hit_permille(0, 100) | 0 |
cache_hit_permille(100, 0) | 0 |
Hint
Integer division gives the floor you need.
Reference solution in Python
def cache_hit_permille(hits: int, requests: int) -> int:
if requests <= 0:
return 0
return hits * 1000 // requests