Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi guys,
I am trying to create student groups/teams based on the preferences.

These are the rules i have to follow to create teams.

First i will check if "Stud A" has added "Stud B" in his Preferences list, and "stud B" has also added "stud A" in his preference, if both of them have added each other i want to add them in group/team (Do this for all students). Second, Check for one sided preferences, "Stud A" has added "Stud B" in his Preferences but stud B hasn't, and add them in group. Third add all students without preference to random Groups. Each group/team can have 5-8 students.

Someone has written this algorithm for me, but i am having hard time understanding this code. Can anyone please explain what is actually happening in this code.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TeamGroup
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, List<string>> AllStudentsPreferences = new Dictionary<string, List<string>>();

            AllStudentsPreferences.Add("A", new List<string>() { "B", "C", "H" });
            AllStudentsPreferences.Add("B", new List<string>() { "A", "C", "D" });
            AllStudentsPreferences.Add("C", new List<string>() { "E", "F", "G" });
            AllStudentsPreferences.Add("D", new List<string>() { "B", "G", "C" });
            AllStudentsPreferences.Add("E", new List<string>() { "F", "G" });
            AllStudentsPreferences.Add("F", new List<string>() { "G", "F", "C"});
            AllStudentsPreferences.Add("G", new List<string>() { "G", "F","A" });
            AllStudentsPreferences.Add("H", new List<string>() { "E", "F" });


            Dictionary<string, List<string>> twoSidedics = new Dictionary<string, List<string>>();
            Dictionary<string, List<string>> oneSidedics = new Dictionary<string, List<string>>();
            foreach (var item in AllStudentsPreferences)
            {
                foreach (var v in item.Value)
                {
                    foreach (var perference in AllStudentsPreferences)
                    {
                        if(item.Key!=perference.Key){
                            if (perference.Key == v)
                            {
                                for (int i = 0; i < perference.Value.Count; i++)
                                {
                                    if (item.Key == perference.Value[i])
                                    {
                                            twoSidedics.Add(item.Key, new List<string> { v });
                                        perference.Value.RemoveAt(i);
                                        i--;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //Console.WriteLine(twoSidedics.Count.ToString());
            //foreach (var item in AllStudentsPreferences)
            //{
            //    string s =string.Join(",",item.Value.ToArray());
            //    Console.WriteLine(item.Key+" "+s);
            //}

            Dictionary<string, List<string>> din = new Dictionary<string, List<string>>();
            foreach (var item in twoSidedics)
            {
                List<string> list = item.Value;

                for (int i = 0; i < item.Value.Count; i++)
                {
                    foreach (var d in twoSidedics)
                    {
                        if (item.Value[i] == d.Key)
                        {
                            foreach (var v in d.Value)
                            {
                                if (list.Contains(v) == false)
                                {
                                    list.Add(v);
                                }
                            }
                            d.Value.Clear();
                        }

                    }

                }
                if (list.Count > 0)
                {
                    din.Add(item.Key, list);
                }
            }


            Dictionary<string, List<string>> dinct = new Dictionary<string, List<string>>();

            foreach (var item1 in din)
            {
                List<string> list = new List<string>();
                bool flag = false;
                foreach (var v1 in item1.Value)
                {
                    foreach (var item2 in din)
                    {
                        if (item1.Key != item2.Key)
                        {
                            foreach (var v2 in item2.Value)
                            {
                                if(v1==v2){
                                    flag = true;
                                    list.Add(item2.Key);
                                }
                            }
                            if(flag){
                                item2.Value.Clear();
                            }
                        }
                    }
                }
                if(flag&& item1.Value.Count>0){
                    list.AddRange(item1.Value);
                    dinct.Add(item1.Key,list);
                }
            }

            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
            foreach (var item in AllStudentsPreferences)
            {
                bool flag = false;
                foreach (var di in din)
                {
                    if (item.Key == di.Key || di.Value.Contains(item.Key))
                    {
                        flag = true;
                    }
                }

                if (flag == false)
                {
                    dic.Add(item.Key, item.Value);
                }
            }
            Dictionary<string, List<string>> din2 = new Dictionary<string, List<string>>();
            foreach (var item in dic)
            {
                List<string> list = item.Value;

                for (int i = 0; i < item.Value.Count; i++)
                {
                    foreach (var d in dic)
                    {
                        if (item.Value[i] == d.Key)
                        {
                            foreach (var v in d.Value)
                            {
                                if (list.Contains(v) == false)
                                {
                                    list.Add(v);
                                }
                            }
                            d.Value.Clear();
                            break;
                        }
                    }

                }
                if (list.Count > 0)
                {
                    din2.Add(item.Key, list);
                }
            }

            foreach (var item in dinct)
            {
                string s = string.Join(",", item.Value.ToArray());
                Console.WriteLine(item.Key + " " + s);
            }
            foreach (var item in din2)
            {
                string s = string.Join(",", item.Value.ToArray());
                Console.WriteLine(item.Key + " " + s);
                Console.ReadKey();
            }

        }
    }
}
Posted
Comments
Ron Beyer 11-Oct-13 10:33am    
Whoever wrote that for you needs to take a lesson in variable naming, its difficult to figure out without pen and paper or going through and trying to figure out what means what.
Mubshir 11-Oct-13 10:40am    
That's why i asked for help man.
Ron Beyer 11-Oct-13 10:49am    
I understand, I was just commenting. I'm hoping somebody has the time to go through this for you, unfortunately I don't today.

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

Ask the person who wrote it: they should have a good idea of your abilities and be able to explain it at a level you understand.
 
Share this answer
 
v2
Comments
Mubshir 11-Oct-13 11:16am    
I am not really after explanation of the whole code. I am after something like,For Example
foreach (var item in AllStudentsPreferences) //this for each loop is listing all students
{
}
Andreas Gieriet 11-Oct-13 14:17pm    
As soon as the code need such comments you know that it needs refactoring.
Replace each such construct (with its potential comment) by a function call with a descriptive name, et-voila, you have a far better code already ;-)
Repeat until no need for explaining the code in such comments.
Of course, you will have comment, e.g. each function needs a description of its purpose (e.g. what is does in general, what it returns in corner cases, etc.).
Try this approach and you have a first exercise in refactoring.
Cheers
Andi
thatraja 11-Oct-13 11:27am    
/*Every single line needs a paragraph of explanation!*/
Only you could, please do it :D 5!
This code has many flaws. The first one is, it is difficult to read: classes seem not to be known, functions neither.
Don't fix broken code, rewrite it.

So, how about invent yourself the implementation of the task at hand. Did you understand the task at all? That's where it starts. Try to rewrite it in you own words and ask question on the spot. E.g.
1) find three kind of people and group them in teams of 5-8 people
2) one kind of people are friends, the other follower, the third outsiders
3) friends are mutually referenced people
4) followers are unilateral connections
5) outsiders have no contacts

What if the sets of kind of people have less persons than makes a group?
Do the groups have to be evenly sized?
...

From that try to think of data and instructions and write them down in pseudo code. E.g.
class Person
{
   variable relations as list of Persons
   function isFriend of a Person P { myself isFollower of P and P isFollower of myself }
   function isFollower of a Person P { P is one of my relations }
   function add Person P { make P being one of my relations unless P is myself }
}

variable comunity is a set of Persons that have a certain realtion
variable friends is a set of Persons given by Friends from the comunity
variable followers is a set of Persons given by Followers from the comunity
variable outsiders is a set of Persons given by Outsiders from the comunity

function Friends of the community
{ member of the community where at least one Person of the comunity is a friend of that member }

function Followers of the community
{ member of the community where at least one Person of the comunity is a follower of that member }

function Outsiders of the community
{ members of the community where no Person of the community is neither friend nor follower of that member }

Write Groups of friends
Write Groups of followers
Write Groups of outsiders

function Write ...

fucntion Groups of a set of Persons
{
  if set is less that 5 person, take these persons,
  otherwise split set into groups of 8, 7, 6, 5 until the rest if the splitting is 0 or larger than 4
}


Transform this in classes and methods of the target language.

Now you see that the code of your question does not show anything of the decomposition shown here.
No structure in data nor code.
No meaningful naming of the variables (give logic names not type-derived names, e.g. not dict, but rather friends, etc.)

Cheers
Andi
 
Share this answer
 
Comments
thatraja 12-Oct-13 1:28am    
So you had more free time to post this? 5!
Andreas Gieriet 12-Oct-13 2:35am    
First of all: thanks for your 5!
Well, yes and no ;-) BTW: It was not me tellig to have no free time to do so... (or do you refer to one of my posts a few days ago? If so, do you know this situation where you should do something but aim for the quick success on another unimportant thing? I fall sometimes into that trap ;-)). On topic: Commenting that code was fruitless anyways.
I think students should learn
- rephrase the task in their words
- decompose problems top-down
- transform into some pseudo code (by avoiding nested loops/ifs as much as possible and have functions defined instead)
- transform pseudo code into any target language
For some reason, I observe many students jumping straight to the details and produce a jungle of obscure loops and ifs and special cases and globally shared state/variables, etc. You name it ;-)
Cheers
Andi
thatraja 12-Oct-13 2:52am    
Yep, you right. I have a plan to make a guide regarding "Posting questions". I know that already couple of guides here but I want to make the guide in different format. Lets see
Cheers!
Mubshir 14-Oct-13 12:42pm    
"This code has many flaws. The first one is, it is difficult to read: classes seem not to be known, functions neither."
Did you See any function calls or class instances in the code?
Andreas Gieriet 14-Oct-13 15:05pm    
This code uses class instances and calls methods on these, but there is no attempt made to decently structure the own code. That's what I say with this statement. Classes and functions/methods are the means to construct software that can be understood by others and yourself.
Uncommented and not decently structured code with e.g. repeatedly multiple nested for-loops is severly broken. Dump it. You may start from scratch and construct your own version from the given assignment. Or you may reverse engineer and try from the innermost to the outermost for-loop to comment what it does and then immediately replace the cmmented code by a function with an appropriate name that encapsulates the for-loop. You will see that the various loops have similar functions. Then try to combine these functions into a class that encapsulates the data plus it's associated functions.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900