PIXELBANKv9.1.0
Menu

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:

Input:
print(hit_rate([True, False, True, True]))
Output:
0.75
Reasoning:
  • The input list contains 4 elements, so the total number of lookups is 4.
  • We count the hits by identifying the True values 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: 34=0.75\frac{3}{4} = 0.75.
  • We round the result to 4 decimal places, which remains 0.75000.7500.
  • The final output is 0.75

Constraints:

  • Rate = hits/total; empty list -> 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.