下式给出:
var args = new string[] { "-one", "two", "three", "-four" };
为了使下面的传递,魔术函数需要看起来是什么样的?
var result = MagicFunction(args);
Assert.AreEqual(0, result[0]);
Assert.AreEqual(3, result[1]);
Assert.AreEqual(2, result.Length);
答案 0 :(得分:2)
int[] MagicFunction(string[] args)
{
return args.Select((s, i) => new { Value = s, Index = i }) // Associate an index to each item
.Where(o => o.Value.StartsWith("-")) // Filter the values
.Select(o => o.Index) // Select the index
.ToArray(); // Convert to array
}
答案 1 :(得分:0)
看起来原型可以为你做到: http://www.prototypejs.org/api/enumerable/find