How to resolve the algorithm Ranking methods step by step in the Python programming language
How to resolve the algorithm Ranking methods step by step in the Python programming language
Table of Contents
Problem Statement
The numerical rank of competitors in a competition shows if one is better than, equal to, or worse than another based on their results in a competition. The numerical rank of a competitor can be assigned in several different ways.
The following scores are accrued for all competitors of a competition (in best-first order): For each of the following ranking methods, create a function/method/procedure/subroutine... that applies the ranking method to an ordered list of scores with scorers:
See the wikipedia article for a fuller description. Show here, on this page, the ranking of the test scores under each of the numbered ranking methods.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ranking methods step by step in the Python programming language
Explanation of the Python Code:
This Python code defines five ranking functions (mc_rank
, sc_rank
, d_rank
, o_rank
, and f_rank
) and demonstrates their usage for ranking a list of scores.
1. Ranking Functions:
The code defines five ranking functions:
mc_rank
: Modified competition ranking.sc_rank
: Standard competition ranking.d_rank
: Dense ranking.o_rank
: Ordinal ranking.f_rank
: Fractional ranking.
These functions take an iterable (such as a list) of tuples as input, where each tuple represents a score and a name. They yield tuples containing a rank (position) and the corresponding score and name.
Implementation of Ranking Functions:
-
mc_rank:
- Keeps track of the last result and a FIFO (first-in, first-out) queue.
- If the current score matches the last result, it's added to the queue.
- If the current score is different, it iterates over the queue, yielding the rank and the queued scores.
- Updates the last result and the queue.
-
sc_rank:
- Keeps track of the last result and the last rank.
- If the current score matches the last result, it yields the last rank and the score.
- Otherwise, it yields the current rank and updates the last result and last rank.
-
d_rank:
- Keeps track of the last result and the last rank (starting as
start-1
). - If the current score matches the last result, it yields the last rank and the score.
- Otherwise, it updates the last result and last rank, then yields the current rank and the score.
- Keeps track of the last result and the last rank (starting as
-
o_rank:
- Simply enumerates the iterable, starting from
start
.
- Simply enumerates the iterable, starting from
-
f_rank:
- Keeps track of the last result and a FIFO queue.
- If the current score doesn't match the last result, it calculates the mean rank of the previous group of scores with the same result.
- Updates the last result and adds the current score to the queue.
Usage of Ranking Functions:
The code includes a list of scores, each represented as a tuple of the score and the score owner's name. It then demonstrates the usage of the ranking functions by iterating over the scores and printing the rank and the corresponding score and name for each function.
Output:
The code outputs the ranked scores using the five different ranking methods. This shows how each ranking method assigns different rankings to the scores based on their scoring criteria and the rules defined in each ranking function.
Source code in the python programming language
def mc_rank(iterable, start=1):
"""Modified competition ranking"""
lastresult, fifo = None, []
for n, item in enumerate(iterable, start-1):
if item[0] == lastresult:
fifo += [item]
else:
while fifo:
yield n, fifo.pop(0)
lastresult, fifo = item[0], fifo + [item]
while fifo:
yield n+1, fifo.pop(0)
def sc_rank(iterable, start=1):
"""Standard competition ranking"""
lastresult, lastrank = None, None
for n, item in enumerate(iterable, start):
if item[0] == lastresult:
yield lastrank, item
else:
yield n, item
lastresult, lastrank = item[0], n
def d_rank(iterable, start=1):
"""Dense ranking"""
lastresult, lastrank = None, start - 1,
for item in iterable:
if item[0] == lastresult:
yield lastrank, item
else:
lastresult, lastrank = item[0], lastrank + 1
yield lastrank, item
def o_rank(iterable, start=1):
"""Ordinal ranking"""
yield from enumerate(iterable, start)
def f_rank(iterable, start=1):
"""Fractional ranking"""
last, fifo = None, []
for n, item in enumerate(iterable, start):
if item[0] != last:
if fifo:
mean = sum(f[0] for f in fifo) / len(fifo)
while fifo:
yield mean, fifo.pop(0)[1]
last = item[0]
fifo.append((n, item))
if fifo:
mean = sum(f[0] for f in fifo) / len(fifo)
while fifo:
yield mean, fifo.pop(0)[1]
if __name__ == '__main__':
scores = [(44, 'Solomon'),
(42, 'Jason'),
(42, 'Errol'),
(41, 'Garry'),
(41, 'Bernard'),
(41, 'Barry'),
(39, 'Stephen')]
print('\nScores to be ranked (best first):')
for s in scores:
print(' %2i %s' % (s ))
for ranker in [sc_rank, mc_rank, d_rank, o_rank, f_rank]:
print('\n%s:' % ranker.__doc__)
for rank, score in ranker(scores):
print(' %3g, %r' % (rank, score))
You may also check:How to resolve the algorithm Averages/Mode step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Peano curve step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Undefined values step by step in the Java programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Stata programming language