SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Location
    Stocholm, Sweden
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making a tournament list - C# (or VB.NET/JAVA)

    Hey,

    I'm starting to get the hang of C#, but once in a while I get to a point where I can't make my head to do what I want.

    I hope someone can help me with this little problem I'm having. I have a list of names (String[]) and I'm trying to make a paired list of all the names so everybody is paired with everybody once.

    This is what I have come up with so far:
    http://pastebin.com/f6f1a666

    The result:
    http://pastebin.com/f7bb00bb5

    I do believe the content of the list is correct, but to make the list more usable for it's purpose (a student tournament) the names should be spread evenly all over the list. Can someone please help me with that? Been stuck all day, and I'm starting to get a headache from all the thinking.

    Kind regards,
    bajas from sweeden
    Last edited by Bajas; Aug 13, 2007 at 00:58.
    I feel obligated to say that I知 a newbie so be patient with me.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is probably the best use of MOD.

    If you don't know already, MOD (in my own view) is a way to check if a number to a multiple of another number.

    Code:
    protected void MakeList() {
    
        String[] names = new String[] 
            {"John",
            "Jack",
            "Anna",
            "Lisa",
            "Sean",
            "Peter",
            "Paul",
            "Tom",
            "Kenneth",
            "Pablo",
            "Christian",
            "Andre"};
    
        ArrayList namesPaired = new ArrayList();
    
        for(int i = 0; i < names.length; i++) {
            if((i &#37; 2) == 0) {
                namesPaired.Add(String.Format("{0} and {1}", names[i], names[i+1]));
            }
        }
    
        foreach(string name in namesPaired) {
            Console.WriteLine(name);
        }
    
    }
    When ever I is a multple of 2, it will add i and i+1 as a string to the ArrayList.


  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Location
    Stocholm, Sweden
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the heads up!

    I'm familiar with the mod operator (modulus), but I didn't think of using it in this situation. As your example demonstrates, the mod will, used like this, tell us if the number is odd or even. The remainder is 0, ergo it is an even number.

    Anyways; That really doesn't help me with my problem. As said in the topic I need all the members to be paired up with all the other members once, and only once. As demonstrated with my code I have a working solutions for this, but I need the names to be spread evenly all through the list.

    With 12 names, the grand total should be 66 pairs.

    I guess there is a logical/mathematical solutions to the problem, but I'm out of bounds on this one.

    So please feel free to help me in any ways you can.

    Kind regards,
    bajas
    Last edited by Bajas; Aug 13, 2007 at 00:59.
    I feel obligated to say that I知 a newbie so be patient with me.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah sorry, I didn't realise you required it in this way.

    I can only come up with the following which is pretty much the same as yours?

    Code:
    protected void MakeList() {
    
        String[] names = new String[] 
            {"John",
            "Jack",
            "Anna",
            "Lisa",
            "Sean",
            "Peter",
            "Paul",
            "Tom",
            "Kenneth",
            "Pablo",
            "Christian",
            "Andre"};
    
        ArrayList namesPaired = new ArrayList();
    
        for(int i = 0; i < names.length; i++) {
            
            for(int j = 0; j < names.length; i++) { 
                
                if(names[i] != names[j])
                    namesPaired.Add(String.Format("{0} and {1}", names[i], names[j]));
    
            }
    
        }
    
        Console.WriteLine(namesPaired.Count.ToString());
    
        foreach(string name in namesPaired) {
            Console.WriteLine(String.Format("{0}<br />", name));
        }
    
    }
    All of this is untested as I haven't got anything installed at the mo to test it. Just installed Vista, and i'm having a play before installing all the software i need.


  5. #5
    SitePoint Member
    Join Date
    Feb 2005
    Location
    Stocholm, Sweden
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I really appreciate the effort, but you're still one step behind me. In your example everybody is paired with everyone twice and not once as intended: http://pastebin.com/f658543ca
    I feel obligated to say that I知 a newbie so be patient with me.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about this?

    Code:
    144
    
    Err: John and John
    John and Jack
    John and Anna
    John and Lisa
    John and Sean
    John and Peter
    John and Paul
    John and Tom
    John and Kenneth
    John and Pablo
    John and Christian
    John and Andre
    Err: Jack and John
    Err: Jack and Jack
    Jack and Anna
    Jack and Lisa
    Jack and Sean
    Jack and Peter
    Jack and Paul
    Jack and Tom
    Jack and Kenneth
    Jack and Pablo
    Jack and Christian
    Jack and Andre
    Err: Anna and John
    Err: Anna and Jack
    Err: Anna and Anna
    Anna and Lisa
    Anna and Sean
    Anna and Peter
    Anna and Paul
    Anna and Tom
    Anna and Kenneth
    Anna and Pablo
    Anna and Christian
    Anna and Andre
    Err: Lisa and John
    Err: Lisa and Jack
    Err: Lisa and Anna
    Err: Lisa and Lisa
    Lisa and Sean
    Lisa and Peter
    Lisa and Paul
    Lisa and Tom
    Lisa and Kenneth
    Lisa and Pablo
    Lisa and Christian
    Lisa and Andre
    Err: Sean and John
    Err: Sean and Jack
    Err: Sean and Anna
    Err: Sean and Lisa
    Err: Sean and Sean
    Sean and Peter
    Sean and Paul
    Sean and Tom
    Sean and Kenneth
    Sean and Pablo
    Sean and Christian
    Sean and Andre
    Err: Peter and John
    Err: Peter and Jack
    Err: Peter and Anna
    Err: Peter and Lisa
    Err: Peter and Sean
    Err: Peter and Peter
    Peter and Paul
    Peter and Tom
    Peter and Kenneth
    Peter and Pablo
    Peter and Christian
    Peter and Andre
    Err: Paul and John
    Err: Paul and Jack
    Err: Paul and Anna
    Err: Paul and Lisa
    Err: Paul and Sean
    Err: Paul and Peter
    Err: Paul and Paul
    Paul and Tom
    Paul and Kenneth
    Paul and Pablo
    Paul and Christian
    Paul and Andre
    Err: Tom and John
    Err: Tom and Jack
    Err: Tom and Anna
    Err: Tom and Lisa
    Err: Tom and Sean
    Err: Tom and Peter
    Err: Tom and Paul
    Err: Tom and Tom
    Tom and Kenneth
    Tom and Pablo
    Tom and Christian
    Tom and Andre
    Err: Kenneth and John
    Err: Kenneth and Jack
    Err: Kenneth and Anna
    Err: Kenneth and Lisa
    Err: Kenneth and Sean
    Err: Kenneth and Peter
    Err: Kenneth and Paul
    Err: Kenneth and Tom
    Err: Kenneth and Kenneth
    Kenneth and Pablo
    Kenneth and Christian
    Kenneth and Andre
    Err: Pablo and John
    Err: Pablo and Jack
    Err: Pablo and Anna
    Err: Pablo and Lisa
    Err: Pablo and Sean
    Err: Pablo and Peter
    Err: Pablo and Paul
    Err: Pablo and Tom
    Err: Pablo and Kenneth
    Err: Pablo and Pablo
    Pablo and Christian
    Pablo and Andre
    Err: Christian and John
    Err: Christian and Jack
    Err: Christian and Anna
    Err: Christian and Lisa
    Err: Christian and Sean
    Err: Christian and Peter
    Err: Christian and Paul
    Err: Christian and Tom
    Err: Christian and Kenneth
    Err: Christian and Pablo
    Err: Christian and Christian
    Christian and Andre
    Err: Andre and John
    Err: Andre and Jack
    Err: Andre and Anna
    Err: Andre and Lisa
    Err: Andre and Sean
    Err: Andre and Peter
    Err: Andre and Paul
    Err: Andre and Tom
    Err: Andre and Kenneth
    Err: Andre and Pablo
    Err: Andre and Christian
    Err: Andre and Andre
    Thats my output, I have put Err: to show where the code finds a pair that is already "paired".

    Code:
            static void makeList()
            {
                String[] names = new String[] 
            {
                    "John",
                "Jack",
                "Anna",
                "Lisa",
                "Sean",
                "Peter",
                "Paul",
                "Tom",
                "Kenneth",
                "Pablo",
                "Christian",
                "Andre"};
    
                ArrayList pairs = new ArrayList();
    
                for(int i = 0; i < names.Length; i++)
                {
                    for(int j = 0; j < names.Length; j++)
                    {
                        if(!names[i].Equals(names[j]) && !hasPair(names[i], names[j], pairs))
                            pairs.Add(String.Format("{0} and {1}", names[i], names[j]));
                        else
                            pairs.Add(String.Format("Err: {0} and {1}", names[i], names[j]));
                    }
                }
    
                Console.WriteLine(pairs.Count);
                Console.WriteLine();
                foreach(string name in pairs)
                {
                    Console.WriteLine(name);
                }
                Console.ReadLine();
            }
            static bool hasPair(string name1, string name2, ArrayList items)
            {
                foreach(string name in items)
                {
                    if(name.Equals(String.Format("{0} and {1}", name2, name1)))
                        return true;
                }
                return false;
            }
    HTH


  7. #7
    SitePoint Member
    Join Date
    Feb 2005
    Location
    Stocholm, Sweden
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tnx once again, gRoberts

    But you're not quite there yet. Your code now does exactly what mine does. The problem is spreading the names/pairs evenly in the list. For the list to be "fair" all the pairs should have the same amount of days/time between every entry. As you can see John has all his entries in sequence at the top of the list.

    It should have been like this:

    John and Jack
    Anna and Lisa
    Sean and Peter
    Paul and Tom
    Kenneth and Pablo
    Christian and Andre
    John and Anna
    Jack and Lisa
    Sean and Paul
    Peter and Tom
    Tom and Pablo
    Kenneth and Christian
    Andre and John

    etc etc...

    You see the problem?
    I feel obligated to say that I知 a newbie so be patient with me.

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I think you might want to separate problems here. First issue is getting correct pairings, second issue is randomizing the order. Easiest way to do something is make your pairings an object with, say, 3 properties (LeftPerson, RightPerson, Sort). Make sort a random value, and implement IComparable using the Sort property. Then just build a List<PersonPair> [nb: ArrayLists are archaic] and call Sort() once all is said and done.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fun

    Code Csharp:
     
            List<string> q = new List<string>(names);
            List<string> pairs = new List<string>(names.Length * (names.Length - 1));
     
            while (q.Count > 0)
            {
                string n1 = q[0];
                q.RemoveAt(0); // name1 was next in line
     
                // try to pair with the first in line who have not been paired with this student before
                foreach (string n2 in q)
                {
                    // create a normalized "candidate" pair with the next student in line
                    string p = string.Compare(n1, n2) < 0 ? n1 + " and " + n2 : n2 + " and " + n1;
     
                    if (!pairs.Contains(p))
                    {
                        // the two students have not been paired before
                        pairs.Add(p);
                        q.Remove(n2);
                        q.Add(n1);
                        q.Add(n2);
                        // the pair is recorded and the students have been moved to the end of the line
                        break;
                    }
                }
            }

  10. #10
    SitePoint Member
    Join Date
    Feb 2005
    Location
    Stocholm, Sweden
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, honeymonster.

    That is exactly what I was looking for. I owe you one...
    I feel obligated to say that I知 a newbie so be patient with me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •