MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / assign_ranks

Function assign_ranks

maths/spearman_rank_correlation_coefficient.py:4–24  ·  view source on GitHub ↗

Assigns ranks to elements in the array. :param data: List of floats. :return: List of ints representing the ranks. Example: >>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1]) [3, 1, 4, 2, 5] >>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0]) [3, 1, 5, 2, 4]

(data: Sequence[float])

Source from the content-addressed store, hash-verified

2
3
4def assign_ranks(data: Sequence[float]) -> list[int]:
5 """
6 Assigns ranks to elements in the array.
7
8 :param data: List of floats.
9 :return: List of ints representing the ranks.
10
11 Example:
12 >>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1])
13 [3, 1, 4, 2, 5]
14
15 >>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0])
16 [3, 1, 5, 2, 4]
17 """
18 ranked_data = sorted((value, index) for index, value in enumerate(data))
19 ranks = [0] * len(data)
20
21 for position, (_, index) in enumerate(ranked_data):
22 ranks[index] = position + 1
23
24 return ranks
25
26
27def calculate_spearman_rank_correlation(

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected