Open the context
Choose the project folder and name the files or behavior involved.
Assign one-based average ranks to at most 1000 finite numeric values while preserving their input positions. Check ties, negative values and empty input, rejecting Boolean values and non-finite floats before sorting.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import isfinite
def average_ranks(values):
if type(values) not in (list, tuple) or len(values) > 1000:
raise ValueError("values.container")
values = tuple(values)
if len(values) > 1000 or any(
type(v) not in (int, float) or
(type(v) is float and not isfinite(v)) for v in values
):
raise ValueError("values.invalid")
order = sorted(range(len(values)), key=values.__getitem__)
result = [0.0] * len(values)
first = 0
while first < len(order):
end = first + 1
while end < len(order) and values[order[end]] == values[order[first]]:
end += 1
rank = (first + 1 + end) / 2
for i in order[first:end]:
result[i] = rank
first = end
return resultThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.