检测对象列表中的转换

时间:2016-03-11 22:58:26

标签: c# algorithm detection

我有一个有序的对象列表(名为LO) 每个对象(名为Ob)是一个或多个int的列表?元素(List <int?>),例如:<30> or <30, null> or <null> or <28, 30>等。编辑:在此列表中,每个元素都是唯一的。

我需要检测(并计算)对象列表中的转换,转换是LO列表中对象顺序的某个序列。 (编辑:我使用->来表示下一个对象。例如<A> -> <B>是对象列表中的2个对象LO:说第3个是<A>并且第四个是<B>

<A> -> <B> :transition
<A> -> <null> -> <B> or  <A> -> <null> ->...-> <null> -> <B> : transition
<A> -> <A,B> -> <B or  <A> -> <A,B> ->...-> <A,B> ->B :transition
<A> -> <null> -> <A> : transition

<A> -> <AB> -> <A>是例外,它不是过渡,当然重复<A>-><A><null> -> <null><A,B> -> <A,B>等的任何组合都不是过渡

编辑:仅转换以单个对象(如)开头并以单个对象(如)结束的序列。 - &GT; - &GT;不是一系列标识过渡的元素。

我该怎么做?我的想法是将A -> (*)A -> A模式检测为异常。我应该预先过滤列表以排除重复数据吗?

1 个答案:

答案 0 :(得分:0)

好的,我创建了一个程序(也可在dotnetfiddle.com上找到),我试图满足您的要求。但我仍然认为你的规格不清楚。

这就是我实施的目标:

  • 对象列表(LO)可以包含对象(OB)。
  • OB)是String的列表(在您的方案中必须替换为int?)。
  • OB被视为Set。允许重复,但忽略。
  • 如果
  • OB包含相同的Strings
  • ,则它们是相同的
  • LO为空时,转换为0
  • LO仅包含一个OB时,转换为0
  • OB中的第一个OB或最后一个LO有多个String时,转换为0
  • 当两个后续OB不相同时,转换次数增加1。

我在输出中添加了一些评论,因为我不明白你的意思。

<强>输出

[A] -> [B] : 1

[A] -> [null] -> [B] : 2

[A] -> [null] -> [null] -> [null] -> [null] -> [B] : 2

[A] -> [A,B] -> [B] : 2

[A] -> [null] -> [A] : 2

[A] -> [A,B] -> [A] : 2
Why is this not a transition but an exception?

[A] -> [A,null] -> [A,null,null] -> [A] : 2
[A,null] and [A,null,null] are considered equal as per your second comment.

[A] -> [A,B,null,C] -> [A] : 2
As per your first comment.

[A] -> [A,B] -> [B,A] -> [A] : 2
Second and third OB are equal as per your first comment.

[A] -> [null,A] -> [B] : 2

[A] -> [A,B,C] -> [B] : 2
Why is this not a transition as per your fourth comment?

源代码

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static string A = "A";
    public static string B = "B";
    public static string C = "C";
    public static string NULL = "null";

    public static void Main()
    {   
        var lo = new LO();
        lo.Add(new OB{A});
        lo.Add(new OB{B});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{NULL});
        lo.Add(new OB{B});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{NULL});
        lo.Add(new OB{NULL});
        lo.Add(new OB{NULL});
        lo.Add(new OB{NULL});
        lo.Add(new OB{B});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,B});
        lo.Add(new OB{B});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{NULL});
        lo.Add(new OB{A});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,B});
        lo.Add(new OB{A});
        lo.DisplayResult("Why is this not a transition but an exception?");


        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,NULL});
        lo.Add(new OB{A,NULL,NULL});
        lo.Add(new OB{A});
        lo.DisplayResult("<A,NULL> and <A,NULL,NULL> are considered equal as per your second comment.");

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,B,NULL,C});
        lo.Add(new OB{A});
        lo.DisplayResult("As per your first comment");

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,B});
        lo.Add(new OB{B,A});
        lo.Add(new OB{A});
        lo.DisplayResult("Second and third OB are equal as per your first comment");

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{NULL,A});
        lo.Add(new OB{B});
        lo.DisplayResult();

        lo.Clear();
        lo.Add(new OB{A});
        lo.Add(new OB{A,B,C});
        lo.Add(new OB{B});
        lo.DisplayResult("Why is this not a transition as per your fourth comment?");
    }
}

// list of objects (LO) = list<OB>
public class LO : List<OB>
{
    public void DisplayResult(string message = null)
    {
        Console.WriteLine("{0} : {1}",this,CountTransitions());
        if(message != null)
        {
            Console.WriteLine(message);
        }
        Console.WriteLine();
    }

    private int CountTransitions()
    {
        if(this.Any()== false || this.Count == 1) return 0;

        var first = this.FirstOrDefault();
        var last = this.LastOrDefault();

        // first OB and last OB must have exactly one element.
        if(first.Count != 1 || last.Count != 1) return 0;

        var transitions = 0;
        for(var i = 0; i < this.Count - 1; i++)
        {
            // when current OB and next OB are not the same, it is a transition
            transitions = IsTransition(this[i],this[i+1]) ? transitions + 1 : transitions;
        }
        return transitions;
    }

    private bool IsTransition(OB lhs, OB rhs)
    {
        var lhsSet = new SortedSet<String>(lhs);
        var rhsSet = new SortedSet<String>(rhs);
        return lhsSet.SetEquals(rhsSet) == false;
    }

    public override string ToString()
    {
        return String.Join(" -> ", this.Select(x => x.ToString()));
    }
}

// objects (OB) = list<int?>
public class OB : List<String>
{
    public override String ToString()
    {
        return String.Format("[{0}]", String.Join(",", this));
    }
}