4
Random r = new Random();
List<object> myList = new List<object>();

for (int i = 0; i < 100; i++)
  myList.Add(new { A = r.Next(), B = r.Next() });

myList.Sort( (obj1, obj2) => obj1.A.CompareTo(obj2.A) );

The above code defines a generic List an populates it with 100 anonymous variables with two members, A and B.

Let's say I want to sort myList on A. The final line attempts to sort the list, however this code clearly doesn't compile because the compiler doesn't know what the list of objects contains.

Without using LINQ, is it possible to somehow sort this list using a lambda or similar?


Having trouble solving an exercise from CodeChef [easy]

So, basically I'm feeling incredibly dumb, because of this exercise, I've spent like 4 or 5 hours trying to code it, and so far I've not been successful.

I have come to the realization that this one is easier to solve with a tree traversal using the Longest Path approach, But I'm not sure (Could you please confirm this to me.), could be over-kill since it's supposed to be one of the easy problems, So Could you please help me with some guidance or basic steps or algorithm approaches on how to solve this one? all kinds of help is certainly appreciated.

PS. I usually post some code about what I've done so far, but I believe that to this point everything has been so wrong that I prefer to start from scratch, at least in terms of ideas.

Thanks.

As per-request, here's the Code I typed according to the accepted answer to solve the Exercise:

def get_max_sum(matrix)
  (1...matrix.length).each do |index|  
    flag = 0  
    matrix[index].each_with_index do |e, i|    
      add = (matrix[index-1][flag] > matrix[index-1][flag+1]) ? matrix[index-1][flag] : matrix[index-1][flag+1]
      e += add
      matrix[index][i] = e
      flag = flag + 1
    end    
  end
  matrix[-1][0]
end

Where the matrix param is an array of arrays, each one representing a row from the triangle.

4

3 回答 3

6

只要您设法使用var语法将列表声明为匿名类型的对象列表,或者愿意使用,就可以在没有 LINQ 的情况下对其进行排序dynamic

Random r = new Random();
List<dynamic> myList = new List<object>();

for (int i = 0; i < 100; i++)
    myList.Add(new { A = r.Next(), B = r.Next() });

myList.Sort( (obj1, obj2) => obj1.A.CompareTo(obj2.A) );
于 2012-05-22T01:28:03.257 回答
2

首先,如果您非常小心地使用隐式类型,则可以使用 LINQ:

var myList = Enumerable.Range(0, 100)
                       .Select(index => new { A = r.Next(), B = r.Next() })
                       .ToList();
myList.Sort( (obj1, obj2) => obj1.A.CompareTo(obj2.A) );

其次,如果您愿意使用dynamic,请将列表键入为a List<dynamic>,然后您可以直接使用LINQ。

但是,我真的不明白您为什么坚持使用匿名类型。只需创建一个标称类型并享受强类型、编译时安全和 LINQ 的荣耀!

于 2012-05-22T01:26:48.700 回答
1

LINQ 没有魔法——您可以查看所有方法签名并了解类型是如何计算出来的。

您想要这样的东西来创建您的类型列表(类似于ToList()扩展名):

List<T> NewList<T>(T ignored)
{
    return new List<T>();
}

并将其用作

Random r = new Random();
var myList = NewList(new { A = r.Next(), B = r.Next() });

for (int i = 0; i < 100; i++)
  myList.Add(new { A = r.Next(), B = r.Next() });

myList.Sort( (obj1, obj2) => obj1.A.CompareTo(obj2.A) );
于 2012-05-22T01:33:45.733 回答