Cache Hit Rate
Problem Statement
Given a stream of cache lookups labeled hit or miss, report the hit rate.
Background
Hit rate is hits / total. With no lookups the rate is defined as 0.0.
Your Task
def hit_rate(lookups):
- lookups: list of booleans (True = hit).
- Return the fraction of hits, rounded to 4 decimals.
Input Format
- lookups (list of bool).
Output Format
- A float rounded to 4 decimals.
Sample
print(hit_rate([True, False, True, True]))
Output:
0.75
Example
print(hit_rate([True, False, True, True]))
0.75
- The input list contains 4 elements, so the total number of lookups is 4.
- We count the hits by identifying the
Truevalues in the list: there are 3 hits (True,True,True). - We calculate the hit rate by dividing the number of hits by the total lookups: .
- We round the result to 4 decimal places, which remains .
- The final output is 0.75
Constraints
- Rate = hits/total; empty list -> 0.0.
- Round to 4 decimals.