lucaseo1991
lucaseo1991

Reputation: 29

Ranking algorithm with win-lose records

I am looking for an algorithmic approach to sort elements based on its win-lose records of each combiniation.

Please take a look at the sample data

('a', 'b') -> (W, L)
('a', 'c') -> (L, W)
('a', 'd') -> (L, W)
('a', 'e') -> (W, L)
('b', 'c') -> (L, W)
('b', 'd') -> (L, W)
('b', 'e') -> (W, L)
('c', 'd') -> (W, L)
('c', 'e') -> (W, L)
('d', 'e') -> (W, L)

The winner is placed right side of the array

ex)

Desired result ordered from Lost -> Win

[e, b, a, d, c]

Is there a keyword, or approach I can chase on to solve this problem?

Upvotes: 0

Views: 307

Answers (3)

Sgnl
Sgnl

Reputation: 1959

I would go about by assigning each token, w, l, (and you could do draw d) a value, such as w=3, l=1, d=2.

Then you would map those values to each player's result and you'd sort it accordingly.

So from your example of:

('a', 'b') -> (W, L)
('a', 'c') -> (L, W)
('a', 'd') -> (L, W)
('a', 'e') -> (W, L)
('b', 'c') -> (L, W)
('b', 'd') -> (L, W)
('b', 'e') -> (W, L)
('c', 'd') -> (W, L)
('c', 'e') -> (W, L)
('d', 'e') -> (W, L)

gets mapped to something like this:

('a', 'b') -> (2, 1)
('a', 'c') -> (1, 2)
('a', 'd') -> (1, 2)
('a', 'e') -> (2, 1)
('b', 'c') -> (1, 2)
('b', 'd') -> (1, 2)
('b', 'e') -> (2, 1)
('c', 'd') -> (2, 1)
('c', 'e') -> (2, 1)
('d', 'e') -> (2, 1)

Sum up the values by their key:

a: 6
b: 4
c: 8
d: 7
e: 4

and sort the values starting with lowest:

b: 4
e: 4
a: 6
d: 7
c: 8

Upvotes: 0

Paddy3118
Paddy3118

Reputation: 4772

This assumes that the ordering is based on number of wins per item.

# -*- coding: utf-8 -*-
"""
https://stackoverflow.com/questions/67630173/ranking-algorithm-with-win-lose-records

Created on Fri May 21 19:00:33 2021

@author: Paddy3118
"""

data = {('a', 'b'): ('W', 'L'),
('a', 'c'): ('L', 'W'),
('a', 'd'): ('L', 'W'),
('a', 'e'): ('W', 'L'),
('b', 'c'): ('L', 'W'),
('b', 'd'): ('L', 'W'),
('b', 'e'): ('W', 'L'),
('c', 'd'): ('W', 'L'),
('c', 'e'): ('W', 'L'),
('d', 'e'): ('W', 'L'),
}

all_items = set()
for (i1, i2) in data.keys():
    all_items |= {i1, i2}       # Finally = {'a', 'b', 'c', 'd', 'e'}

win_counts = {item: 0 for item in all_items}
for (i1, i2), (r1, r2) in data.items():
    if r1 == 'W':
        win_counts[i1] += 1
    else:
        win_counts[i2] += 1

# win_counts = {'a': 2, 'd': 3, 'b': 1, 'e': 0, 'c': 4}

answer = sorted(all_items, key=lambda i: win_counts[i])
print(answer)  # ['e', 'b', 'a', 'd', 'c']

Upvotes: -1

Yongjie Wu
Yongjie Wu

Reputation: 1

I am not a native English speaker, please forgive me if there is any grammatical error.

Record the number of wins and losses for each letter, the letter that has not been lost is the largest letter, and the letter that has not been won is the smallest letter.

This problem can be transformed into the longest path algorithm. You can think of each comparison as a path of length 1, so that the longest path from the smallest letter to the largest letter represents the result.

Upvotes: -1

Related Questions