easy +8 pts

Percentile Calculation

Compute the percentile rank of a value in a list of numbers.

Write a function `percentile_rank(data, value)` that takes a list of numbers `data` and a numeric `value`, and returns the percentile rank of `value` within `data` as a float rounded to two decimal places. The percentile rank is defined as the percentage of data points that are **less than or equal to** `value` (i.e., the number of data points `<= value` divided by the total number of data points, multiplied by 100). If `data` is empty, return `0.0`. Assume all inputs are numbers (int or float). The function should not modify the input list. Examples: - `percentile_rank([1, 2, 3, 4], 3)` returns `75.0` because 3 out of 4 values are <= 3. - `percentile_rank([1, 2, 3, 4], 5)` returns `100.0`. - `percentile_rank([1, 2, 3, 4], 0)` returns `0.0`. - `percentile_rank([], 10)` returns `0.0`.

Constraints

`0 <= len(data) <= 10^4` `-10^6 <= value, each element <= 10^6` Time complexity: O(n) where n = len(data).

Example

>>> percentile_rank([1, 2, 3, 4], 3)
75.0
>>> percentile_rank([1, 2, 3, 4], 5)
100.0
>>> percentile_rank([1, 2, 3, 4], 0)
0.0
>>> percentile_rank([], 10)
0.0
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Count how many elements in `data` are <= `value`.
If `data` is empty, return 0.0.
Multiply the fraction by 100 and round to two decimals.
Use `round(count / len(data) * 100, 2)`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.