开发者

Find out most popular from different results

开发者 https://www.devze.com 2023-02-10 19:29 出处:网络
This is more of a mathematical question than a programming question, but it still includes some programming.

This is more of a mathematical question than a programming question, but it still includes some programming.

This program scrapes the informati开发者_JAVA技巧on from user lists on the IMDB website, and is supposed to display the most popular celebrity of all of the inputted lists. This works, just displays inaccurate results.

This is what I have:

            public List<Star[]> stars = new List<Star[]>();

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "Processing...";
        stars.Add(ParseList(textBox1.Text));

        label1.Text = "Waiting.";

        treeView1.Nodes.Clear();

        List<Star> strs = new List<Star>();

        foreach (Star[] stlst in stars)
        {
            foreach (Star s in stlst)
            {
                bool inalrdy = false;

                int index = 0;

                for (int i = 0; i < strs.Count; i++)
                {
                    if (s.Name == strs[i].Name)
                    {
                        inalrdy = true;
                        index = i;
                        break;
                    }
                }

                if (inalrdy)
                {
                    strs[index].Points += s.Points;
                }
                else
                {
                    strs.Add(s);
                }
            }
        }

        foreach (Star s in strs)
        {
            treeView1.Nodes.Add(s.Points + ". " + s.Name);
        }


    }

What would I do to have it rank them accurately even when there is never the same players?

This is very important to me and I would be very grateful for an answer.


You have the relationship of players for each list, but what you want is the relationship of players overall. Assuming that this is solvable (i.e there is information to rank each player and there is no cases where player 1 and 2 has beaten each other)

The algorithm would look something like:

  1. Get the first ranking player in the first list. Traverse through the first element of each remaining list to see if that player is beaten. If yes, replace by the player that beats him, otherwise he's number one.

  2. Remove that player from the list.

  3. Repeat from 1.


OK, so how about some LINQ?

Something like

public class Player
{
    public string Name;
    public double Rank;
}

List<List<Player>> someOtherList = new List<List<Player>>
                                   {
                                       new List<Player> 
                                           {
                                               new Player {Name = "A", Rank = 1}, 
                                               new Player {Name = "B", Rank = 2}
                                           },
                                       new List<Player>
                                           {
                                               new Player {Name = "A", Rank = 2}, 
                                               new Player {Name = "C", Rank = 1}
                                           }
                                   };

List<Player> plrs = 
        (from p in someOtherList.SelectMany(singleList => singleList)
        group p by p.Name into g
         select new Player { Name = g.Key, Rank = g.Average(x => x.Rank) })
         .OrderBy(x => x.Rank)
         .ToList();

foreach (Player p in plrs)
{
    OverallPlayers.Add(p.Rank + ". " + p.Name);
}

EDIT

Changed to look something like the OP code. I would dtill strongly recomend you spend some time on that LINQ tutorial once you feel better...

List<Star[]> stars = new List<Star[]>
                                       {
                                           new[]
                                               {
                                                   new Star {Name = "A", Points = 1}, 
                                                   new Star {Name = "B", Points = 2}
                                               },
                                           new[]
                                               {
                                                   new Star {Name = "A", Points = 2}, 
                                                   new Star {Name = "C", Points = 1}
                                               }
                                       };
List<Star> plrs =
        (from p in stars.SelectMany(singleList => singleList)
        group p by p.Name into g
         select new Star { Name = g.Key, Points = g.Average(x => x.Points) })
         .OrderBy(x => x.Points)
         .ToList();

foreach (Star p in plrs)
{
    //OverallPlayers.Add(p.Rank + ". " + p.Name);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消