

Create a function, this function receives two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is included in list1.
Function signature:
differ_scope(list1, list2)
Parameters:
list1, list2 - list1 and list2 are constructed with strings,
each string indicates a number or a scope of
numbers. The number or scope are randomly, can
be overlapped. All numbers are positive.
E.g.
['23', '44-67', '12', '3', '20-90']
Return Values:
True - if all scopes and numbers indicated by list2 are included in list1.
False - if any scope or number in list2 is out of the range in list1.
Examples:
case1 - list1 = ['23', '44-67', '12', '3', '20-90']
list2 = ['22-34', '33', 45', '60-61']
differ_scope(list1, list2) == True
case2 - list1 = ['23', '44-67', '12', '3', '20-90']
list2 = ['22-34', '33', 45', '60-61', '100']
differ_scope(list1, list2) == False
贴上自己写的代码如下:(备注: python 2.7.6)
def differ_scope(list1, list2): print "list1:" + str(list1) print "list2:" + str(list2) #设置临时存放列表 list1_not_ = [] #用于存放列表1正常的数字值,当然要用int()来转换 list1_yes_ = [] #用于存放列表1中范围值如 44-67 list1_final = [] #用于存放列表1中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] temp1 = [] list2_not_ = [] #用于存放列表2正常的数字值,当然要用int()来转换 list2_yes_ = [] #用于存放列表2中范围值如 44-67 list2_final= [] #用于存放列表2中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] temp2 = [] temp = [] #用于存放列表1,与列表2比较后的列表,从而判断
总结:
1. 这道题关键是想法,如果整成坐标的方式来比较,会很麻烦。
2. 列表转成范围后,如果消除重复项,同样是里面的关键所在。
3. 其次是对列表遍历的操作,同样挺重要。
