tags:

views:

15

answers:

2

I'm working on an assignment that has a plain text file of data.

Each line of text represents a car race. Each line of text has four strings, delimited by commas.

The strings represent a racer name. The first string is the racer that came first, the second got second place etc.

The task we've been given is to read in this file and sort the racers based on their success. We've been given a comparison algorithm to use:

int compareTo(Racer r1, Racer r2)
{
    for (int i = 0; i < r1.positions.length; i++)
    {
        int diff = r1.positions[i] - r2.positions[i];

        if (diff == 0)
        {
            continue;
        }

        return diff;
    }

    return 0;
}

So essentially first place positions take precendence over second place positions, second over third etc.

But there's an issue with this code.

My thoughts would be that a racer who had 100 second places and no firsts would be better ranked than a racer with only one first place.

So this got me to thinking: Wouldn't it make more sense to have a weighting for each position count?

The weighting would be calculated for each Racer:

int weight = w1*positions[0] 
    + w2*positions[1] 
    + w3*positions[2] 
    + w4*positions[3];

But I ran into an issue. How do I calculate the optimum weights for each position count?

Surely I can look at the existing data and calculate the weights from that? My instincts tell me that I should be trying to calculate it based on the ratio of unique winners per position or something similar.

Is there a theorem that can calculate the weights? I figure I can get a couple of bonus points if I can show a better algorithm ;)

Thanks in advance.

+1  A: 

You make a few assumptions in your argument:

My thoughts would be that a racer who had 100 second places and no firsts would be better ranked than a racer with only one first place.

This depends totally on the rules of the race! Determining if 100 2nds is better than 1 1st and 99 4ths comes down to tournament rules.

Also, a better and simpler way of exaplaining your weighted results method would simply to be have points per race. For example, 1st gives 10 points, 2nd gives 8 etc. This would be dependant again on the rules of the race and tournament, which are not specified so it's hard to continue.

If you want to impress your tutor, think about implementing the scoring system as an optional extra argument in the input, "points".

Tom Gullen
+1  A: 

So essentially first place positions take precendence over second place positions, second over third etc.

That's not what the compareTo function calculates. If the function were written more clearly this would be more obvious:

int diff; 
for (int i = 0; i < r1.positions.length; i++) {
    diff = r1.positions[i] - r2.positions[i];
    if (diff != 0) {
        return diff;
    }
}
return 0;

If positions[i] is a racer's ranking in race i, then unless r1 and r2 tied in the first race this function is equivalent to:

return r1.positions[0] - r2.positions[0];

which only ranks two racers on their standing in the first race only. Therefore the weights for subsequent races are effectively 0. Indeed, if there was a first place racer in race 0, that racer will always be higher ranked than all other racers no matter how many races exist.

I'd ask your instructor about this unintuitive comparison function before proceeding.

msw