相对于模数4,订单列表计数为2和3。

3
我有一个列表需要排序,前提是它包含序列中的所有数字:
手动设置似乎不是正确的方法。
我知道如何将列表按升序或降序排序,但模4让我感到困惑。
模4是由于这与网格上的节点相邻有关。
我不知道它们最初被放入列表的顺序,因为它是随机的,并且每次都会更改 - 网格连接的程序生成。
我不想要一个新列表,我只想简单地重新排列当前列表。
列表计数为3:
0、1、2
1、2、3
2、3、0
3、0、1
if (acceptedIndicies.Contains(0) && acceptedIndicies.Contains(1) && acceptedIndicies.Contains(2)) // Top-Right-Down
{
      // Order List in the sequence
      acceptedIndicies[0] = 0;
      acceptedIndicies[1] = 1;
      acceptedIndicies[2] = 2;
}
else if (acceptedIndicies.Contains(1) && acceptedIndicies.Contains(2) && acceptedIndicies.Contains(3)) // Right-Down-Left
{
      // Order List in the sequence
      acceptedIndicies[0] = 1;
      acceptedIndicies[1] = 2;
      acceptedIndicies[2] = 3;   
}
else if (acceptedIndicies.Contains(2) && acceptedIndicies.Contains(3) && acceptedIndicies.Contains(0)) // Down-Left-Top
{
      // Order List in the sequence
      acceptedIndicies[0] = 2;
      acceptedIndicies[1] = 3;
      acceptedIndicies[2] = 0;
}
else if (acceptedIndicies.Contains(3) && acceptedIndicies.Contains(0) && acceptedIndicies.Contains(1)) // Left-Top-Right
{
      // Order List in the sequence
      acceptedIndicies[0] = 3;
      acceptedIndicies[1] = 0;
      acceptedIndicies[2] = 1;
}

2个列表的计数:

0,1

1,2

2,3

3,0

if (acceptedIndicies.Contains(0) && acceptedIndicies.Contains(1))                 
{
      // Order List in the sequence
      acceptedIndicies[0] = 0;
      acceptedIndicies[1] = 1;        
}
else if (acceptedIndicies.Contains(1) && acceptedIndicies.Contains(2))
{
      // Order List in the sequence
      acceptedIndicies[0] = 1;
      acceptedIndicies[1] = 2;
}
else if (acceptedIndicies.Contains(2) && acceptedIndicies.Contains(3))
{
      // Order List in the sequence
      acceptedIndicies[0] = 2;
      acceptedIndicies[1] = 3;
}
else if (acceptedIndicies.Contains(3) && acceptedIndicies.Contains(0))
{
      // Order List in the sequence
      acceptedIndicies[0] = 3;
      acceptedIndicies[1] = 0;
}

C# if you could please :) - Fox Forge
1个回答

1
你可以做以下事情:

  1. Build a valid modulus sequence. That's easy:

     var modulusSequence = Enumerable.Range(0, modulus);
    
  2. Now you need a way of generating all valid modulus sequences of a given modulus and length. Thats easy too, just shift left or right a total of modulus times a valid sequence we happen to know taking the first n elements of every new shifted sequence:

    private static IEnumerable<int> ShiftLeft(IEnumerable<int> sequence)
    {
        if (!sequence.Any())
            yield break;
    
        foreach (var i in sequence.Skip(1))
        {
            yield return i;
        }
    
        yield return sequence.First();
    }
    
    private static IEnumerable<IEnumerable<int>> getAllModulusSequences(int modulus, int length)
    {
        var sequence = Enumerable.Range(0, modulus);
    
        for (var i = 0; i < modulus; i++)
        {
            yield return sequence.Take(length);
            sequence = ShiftLeft(sequence);
        }
    }
    
  3. And now you simply have to check if the given sequence contains all elements of a valid sequence (note that doing it the way around is not correct because you could get false positives if the given sequence has repeating items). If it does, return the valid sequence.

    I do note that I'm not ordering your list, I'm returning a new one or null if the sequence is not valid.

    public static IList<int> GetOrderedSequence(IList<int> sequence, int modulus)
    {
        if (modulus < sequence.Count)
            throw new ArgumentOutOfRangeException(nameof(sequence), "Sequence can not contain more elements than specified modulus.");
    
        foreach (var validSequence in getAllModulusSequences(modulus, sequence.Count))
        {
            if (validSequence.All(item => sequence.Contains(item)))
                return validSequence.ToList();
        }
    
        return null;
    }
    

这种方法比你的方法更快/更高效吗?可能不是。更美观吗?我认为是,但这是有争议的。更灵活吗?哦,是的!

如果您绝对确定只需要检查模数为423长序列,则可以保留您的代码。如果不是,请实现像我展示给您的通用解决方案。


太好了!非常清晰明了,正是我想要的!非常感谢你。 - Fox Forge
@DannyFox 不用谢!我最初是在手机上编写的,所以我稍微重构了一下代码,使其更加简洁。 - InBetween

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接