有人可以为我解释这个C#代码 [英] Can Someone Please explain this C# code for me

查看:64
本文介绍了有人可以为我解释这个C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试根据偏好创建学生团体/团队。



这些是我必须遵循的规则来创建团队。



首先,我将检查是否 Stud A已经在他的偏好列表中添加了Stud B,并且stud B也在他的偏好中添加了stud A,如果他们两个已经相互添加我想将它们添加到组/团队中(请执行此操作)为所有学生)。第二,检查单侧偏好,Stud A在他的偏好中添加了Stud B但是B没有,并将它们添加到组中。第三,将所有学生添加到随机组中。每个小组/团队可以有5-8名学生。



有人为我编写了这个算法,但我很难理解这段代码。任何人都可以解释这段代码中实际发生的事情。



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.

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();
            }

        }
    }
}

推荐答案

你们知道一行一行解释代码的工作量是多少?

每一行都需要一段解释!例如:

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();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分的理由吗?



问问写的人:他们应该很好地了解你的能力,并能够在你理解的水平上解释它。


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.


这段代码有很多缺陷。第一个是,很难读:类似乎不知道,函数也没有。

不要修复损坏的代码,重写它。



那么,如何发明自己手头的任务的实现。你完全理解这项任务了吗?这就是它开始的地方。尝试用你自己的话重写它,并当场提问。例如

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
}





在目标语言的类和方法中进行转换。



现在您看到问题的代码没有显示此处显示的任何分解。

数据和代码中没有结构。

没有有意义的变量命名(给出逻辑名称不是类型派生的名称,例如不是 dict ,而是朋友等等。)



干杯

Andi



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


这篇关于有人可以为我解释这个C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆