代码之家  ›  专栏  ›  技术社区  ›  Henrik P. Hessel

重复LINQ查询

  •  6
  • Henrik P. Hessel  · 技术社区  · 14 年前

    public static List<char> rotate(this List<char> currentList, int periodes) {
        if (periodes != 1) {
            int x = currentList.Count() - 1;
            return rotate(currentList.Skip(x).
                 Concat(currentList.Take(x)).ToList<char>(), periodes - 1);
        }
        return currentList;
    }
    

    原始状态:

    ring = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
    

    的当前结果 ring.rotate(10);

    J A B C D E F G H I
    I J A B C D E F G H
    H I J A B C D E F G
    G H I J A B C D E F
    F G H I J A B C D E      Recursive Steps
    E F G H I J A B C D
    D E F G H I J A B C
    C D E F G H I J A B
    B C D E F G H I J A
    

    A B C D E F G H I J      Result
    

    最好的
    亨里克

    3 回复  |  直到 14 年前
        1
  •  3
  •   BrunoLM    12 年前

    跳过 i 和concat

    public static class Ex
    {
        public static List<char> Rotate(this List<char> c, int i)
        {
            i %= c.Count;
            return c.Skip(i).Concat(c.Take(i)).ToList();
        }
    }
    
    class Program
    {
        static void Main()
        {
            List<char> chars = new List<char>();
    
            for (int i = 65; i < 75; ++i)
            {
                chars.Add((char)i);
            }
    
            var r1 = chars.Rotate(10); // A B C D E F G H I J
            var r2 = chars.Rotate(1); // B C D E F G H I J A
            var r3 = chars.Rotate(101); // B C D E F G H I J A
            var r4 = chars.Rotate(102); // C D E F G H I J A B
    
            Console.ReadLine();
        }
    }
    
        2
  •  3
  •   amarsuperstar    14 年前

    这将产生与原始解决方案相同的结果,并补偿-1问题:

    public static List<char> rotate2(this List<char> currentList, int periodes)
    {
        int start = (currentList.Count - periodes) + 1;
        return currentList.Skip(start).Concat(currentList.Take(start)).ToList();
    }
    

    编辑

    我把它放进了一个控制台应用程序,结果看起来和我一样

    class Program
    {
        static void Main(string[] args)
        {
    
            List<char> s = "ABCDEFGHIJ".ToList();
    
            for (int x = 0; x < 10; x++)
            {
                s.rotate(x+ 1).ForEach(Console.Write);
                Console.WriteLine();
            }
            Console.WriteLine();
            for (int x = 0; x < 10; x++)
            {
                s.rotate2(x + 1).ForEach(Console.Write);
                Console.WriteLine();
            }
    
            Console.ReadLine();
        }
    }
    
    static class so
    {
        public static List<char> rotate(this List<char> currentList, int periodes)
        {
            while (periodes != 1)
            {
                int x = currentList.Count() - 1;
                return rotate(currentList.Skip(x).
                        Concat(currentList.Take(x)).ToList<char>(), periodes - 1);
            }
            return currentList;
        }
    
        public static List<char> rotate2(this List<char> currentList, int periodes)
        {
            int start = (currentList.Count - periodes) + 1;
            return currentList.Skip(start).Concat(currentList.Take(start)).ToList();
        }
    }
    
        3
  •  1
  •   FacticiusVir    14 年前

    要完全使用Linq编写控制台程序,请执行以下操作:

    class Program
    {
        static void Main(string[] args)
        {
            var ring = Enumerable.Range(97, 10).Select(x => (char)x).ToList();
    
            Console.WriteLine(string.Join("\n", Enumerable.Range(1, 10).Select(x => new string(ring.rotate(x).ToArray()))));
    
            Console.ReadLine();
        }
    }