【发布时间】:2018-09-01 14:15:51
【问题描述】:
假设我有一个任意列表[2, 5, 6, 2, 2, 4]
现在给定一个列表,我们称它为列表 A [1,2,3,4,5,6,0].List A 按特定顺序包含任意列表中的所有数字。现在让我们根据列表 A 的顺序排列第一个列表。每个新订单将成为一个新列表。所以结果应该是
[2,5,6]
[2]
[2,4]
另一个例子,如果列表是根据列表 B [4,5,6,0,1,2,3] 的顺序拆分的,那么结果应该是
[2]
[5,6,2]
[2]
[4] <-- the different is that now the (ordered?) list change, the 4 is now belong on the next row.
我想以 LINQ 或函数式方式执行此操作。我在原始问题中发布了一个迭代解决方案,请在您回答或尝试回答此问题之前不要阅读它,因为我不想在答案中引入迭代偏见思维......
Split a list or ordered dates into weeks using linq
或者看下面的迭代答案
var orders = new List<int> { 4, 5, 6, 0, 1, 2, 3 };
var nums = new List<int> {2, 5, 6, 2, 2, 4};
var queue = new Queue<int>(nums);
var results = new List<List<int>>();
while (queue.Count > 0)
{
var subLists = new List<int>();
foreach (var order in orders)
{
if(order == queue.Peek())
subLists.Add(queue.Dequeue());
if (queue.Count == 0)
break;
}
results.Add(subLists);
}
【问题讨论】:
-
为什么不使用实际的代码示例来输入?
-
当
2位于5和listB中的6之后,为什么第二个示例中的第一个列表的第一项包含{2, 5, 6}?不应该是{2},然后是{5, 6}吗? -
请出示您的迭代解决方案代码
-
var 任意 = 新列表
{ 1, 2, 3, 4, 5, 6, 0 }; var listA = new Queue (new List { 2, 5, 6, 2, 2, 4 }); var resultsA = new List - >(); while (listA.Count > 0) resultsA.Add(arbitrary.Where(o => listA.Count != 0 && o == listA.Peek()).Select(o => listA.Dequeue()).ToList( )); // 你需要这样的东西吗?
-
您得到的答案并不符合函数式编程的精神。这个问题没有意义;您声明列表 A 包含任意列表的编号,但在您的示例中并非如此。
标签: c# linq functional-programming