以特定格式组合和加入给定数据

时间:2012-09-28 08:42:22

标签: c# permutation combinatorics

我有以下数据:

YELLOW,SMALL,STRETCH,ADULT,T
YELLOW,SMALL,STRETCH,ADULT,T
YELLOW,SMALL,STRETCH,CHILD,F
YELLOW,SMALL,DIP,ADULT,F
YELLOW,SMALL,DIP,CHILD,F
YELLOW,LARGE,STRETCH,ADULT,T
YELLOW,LARGE,STRETCH,ADULT,T
YELLOW,LARGE,STRETCH,CHILD,F
YELLOW,LARGE,DIP,ADULT,F
YELLOW,LARGE,DIP,CHILD,F
PURPLE,SMALL,STRETCH,ADULT,T
PURPLE,SMALL,STRETCH,ADULT,T
PURPLE,SMALL,STRETCH,CHILD,F
PURPLE,SMALL,DIP,ADULT,F
PURPLE,SMALL,DIP,CHILD,F
PURPLE,LARGE,STRETCH,ADULT,T
PURPLE,LARGE,STRETCH,ADULT,T
PURPLE,LARGE,STRETCH,CHILD,F
PURPLE,LARGE,DIP,ADULT,F
PURPLE,LARGE,DIP,CHILD,F

我需要生成以下几组数据(注意上面数据的列将生成下面的数据)如果有5列,将有4组数据,第一组有单列,第二组有2,第三组用3,依此类推。最后一列被省略,并且是CLASS列,所有数据都与该列连接,数字是上面给定数据中出现的数字。

--------------------------------------------------
1 column
--------------------------------------------------
YELLOW  T   4   
SMALL   T   4   
STRETCH T   8   
ADULT   T   8   
YELLOW  F   6   
SMALL   F   6   
STRETCH F   4   
CHILD   F   8   
DIP F   8   
ADULT   F   4   
LARGE   T   4   
LARGE   F   6   
PURPLE  T   4   
PURPLE  F   6   
--------------------------------------------------
2 columns
--------------------------------------------------
YELLOW  SMALL   T   2   
YELLOW  STRETCH T   4   
YELLOW  ADULT   T   4   
SMALL   STRETCH T   4   
SMALL   ADULT   T   4   
STRETCH ADULT   T   8   
YELLOW  SMALL   F   3   
YELLOW  STRETCH F   2   
YELLOW  CHILD   F   4   
SMALL   STRETCH F   2   
SMALL   CHILD   F   4   
STRETCH CHILD   F   4   
YELLOW  DIP F   4   
YELLOW  ADULT   F   2   
SMALL   DIP F   4   
SMALL   ADULT   F   2   
DIP ADULT   F   4   
DIP CHILD   F   4   
YELLOW  LARGE   T   2   
LARGE   STRETCH T   4   
LARGE   ADULT   T   4   
YELLOW  LARGE   F   3   
LARGE   STRETCH F   2   
LARGE   CHILD   F   4   
LARGE   DIP F   4   
LARGE   ADULT   F   2   
PURPLE  SMALL   T   2   
PURPLE  STRETCH T   4   
PURPLE  ADULT   T   4   
PURPLE  SMALL   F   3   
PURPLE  STRETCH F   2   
PURPLE  CHILD   F   4   
PURPLE  DIP F   4   
PURPLE  ADULT   F   2   
PURPLE  LARGE   T   2   
PURPLE  LARGE   F   3
--------------------------------------------------
3 columns
--------------------------------------------------
YELLOW  SMALL   STRETCH T   2   
YELLOW  SMALL   ADULT   T   2   
YELLOW  STRETCH ADULT   T   4   
SMALL   STRETCH ADULT   T   4   
YELLOW  SMALL   STRETCH F   1   
YELLOW  SMALL   CHILD   F   2   
YELLOW  STRETCH CHILD   F   2   
SMALL   STRETCH CHILD   F   2   
YELLOW  SMALL   DIP F   2   
YELLOW  SMALL   ADULT   F   1   
YELLOW  DIP ADULT   F   2   
SMALL   DIP ADULT   F   2   
YELLOW  DIP CHILD   F   2   
SMALL   DIP CHILD   F   2   
YELLOW  LARGE   STRETCH T   2   
YELLOW  LARGE   ADULT   T   2   
LARGE   STRETCH ADULT   T   4   
YELLOW  LARGE   STRETCH F   1   
YELLOW  LARGE   CHILD   F   2   
LARGE   STRETCH CHILD   F   2   
YELLOW  LARGE   DIP F   2   
YELLOW  LARGE   ADULT   F   1   
LARGE   DIP ADULT   F   2   
LARGE   DIP CHILD   F   2   
PURPLE  SMALL   STRETCH T   2   
PURPLE  SMALL   ADULT   T   2   
PURPLE  STRETCH ADULT   T   4   
PURPLE  SMALL   STRETCH F   1   
PURPLE  SMALL   CHILD   F   2   
PURPLE  STRETCH CHILD   F   2   
PURPLE  SMALL   DIP F   2   
PURPLE  SMALL   ADULT   F   1   
PURPLE  DIP ADULT   F   2   
PURPLE  DIP CHILD   F   2   
PURPLE  LARGE   STRETCH T   2   
PURPLE  LARGE   ADULT   T   2   
PURPLE  LARGE   STRETCH F   1   
PURPLE  LARGE   CHILD   F   2   
PURPLE  LARGE   DIP F   2   
PURPLE  LARGE   ADULT   F   1   
--------------------------------------------------
4 columns
--------------------------------------------------
YELLOW  SMALL   STRETCH ADULT   T   2   
YELLOW  SMALL   STRETCH CHILD   F   1   
YELLOW  SMALL   DIP ADULT   F   1   
YELLOW  SMALL   DIP CHILD   F   1   
YELLOW  LARGE   STRETCH ADULT   T   2   
YELLOW  LARGE   STRETCH CHILD   F   1   
YELLOW  LARGE   DIP ADULT   F   1   
YELLOW  LARGE   DIP CHILD   F   1   
PURPLE  SMALL   STRETCH ADULT   T   2   
PURPLE  SMALL   STRETCH CHILD   F   1   
PURPLE  SMALL   DIP ADULT   F   1   
PURPLE  SMALL   DIP CHILD   F   1   
PURPLE  LARGE   STRETCH ADULT   T   2   
PURPLE  LARGE   STRETCH CHILD   F   1   
PURPLE  LARGE   DIP ADULT   F   1   
PURPLE  LARGE   DIP CHILD   F   1   

所以甚至可以有50列,可以有80列甚至更多。

复杂性还在于可以有0和1而不是单词,我们需要匹配相同的列。

希望你有问题。

我已经制作了以下代码来生成第1列。主要问题是其余的栏目。

for (int i = 0; i < lsClasses.Count; i++)
            {
                for (int j = 0; j < lsUniqueOps.Count; j++)
                {
                    int count = FindInOrignalList(orignalList, lsClasses[i], lsUniqueOps[j]);
                    if (count != 0)
                    {
                        List<string> checkedItem = new List<string>();
                        checkedItem.Add(lsUniqueOps[j][0]);
                        checkedItem.Add(lsUniqueOps[j][1]);
                        checkedItem.Add(lsClasses[i]);
                        checkedItem.Add(count.ToString());
                        lsFirstItemSet.Add(checkedItem);
                    }
                }
            }

--------------------------------------------------

private static int FindInOrignalList(List<List<string>> orignalList, string lsClass, List<string> lsUniqueOp)
        {
            List<List<string>> tmpList = new List<List<string>>();
            int count = 0;
            for (int i = 0; i < orignalList.Count; i++)
            {
                for (int j = 0; j < orignalList[i].Count - 1; j++)
                {
                    if (orignalList[i][j] + "," + orignalList[i][orignalList[i].Count - 1] == lsUniqueOp[0] + "," + lsClass)
                        count++;
                }
            }
            return count;
        }

1 个答案:

答案 0 :(得分:0)

如果我看得正确,你只能列出最后没有“F”市场的出现。你只得到“T”。那是对的吗?如果是这样,你可以从

开始
  1. 删除F'ed的行,因为它们永远不会导致任何upcount,
  2. 完全删除最后一列(因为只有T在那里而且一条线的存在已经暗示其真实性)
  3. 告诉我这是否是一个作业(在这种情况下,你处于初级水平,需要与高级程序员不同的解释)。
  4. 我会假设你是精明的人。然后,您可以应用LINQ和合适的数据结构来计算出现次数。只是我的头脑 - DataSet和公司可能是一个不错的选择。 Dictionary可能是另一个。