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()
# 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]
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 = [11, 12, 13, 14]
def o1 = [ 13, 14,15]
def o2 = o.intersect(o1)
def o1 = [ 13, 14,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
o1 = [13, 14,15]
puts o-o1