[Grooy]List, Map and Range习题

1.Given the list [14, 12, 13, 11], express how we would obtain the List with these elements in descending order.

def list  =[14,12,13,11]
print list.sort().reverse() // output: [14,13,12,11]

如果使用Ruby也是类似的

list  =[14,12,13,11]
# output: [14,13,12,11]
print list.sort().reverse()

 

2.Given the list [1,2,[3,4]], deternmine the efffect of  [1,2,[3,4]].flatten

def o = [1, [2, [3, 4]]]

print o.flatten() // output: [1,2,3,4]  

如果使用Ruby,也是类似的

o = [1, [2, [3, 4]]]

print o.flatten() # output: [1,2,3,4] 

 

 

3. Given two Lists [11, 12, 13, 14] and [13, 14, 15], how would we obtain the list of items from the first that are not in the second, that is, [11, 12]?

def o = [11121314]
def o1 =  [ 1314,15]
def  o2 = o.intersect(o1)

print o-o2  // output: [11,12]

如果是使用Ruby也是类似的

o = [11, 12, 13, 14]
o1 = [13, 14,15]

puts o-o1

 

posted @ 2012-09-22 09:27  卜海清  阅读(282)  评论(0编辑  收藏  举报