Created
February 23, 2012 07:39
-
-
Save e7h4n/1891339 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding=utf-8 | |
''' | |
找出一个锯齿数组里长度大于5的子数组 | |
在符合要求的子数组里的数据里找出所有偶数 | |
如果数据小于10的话乘以2,大于10的除以2 | |
最后统计符合要求的数据的和 | |
''' | |
inputdata = [ | |
[2,8,9,13,72,67,88,35,44], | |
[33,28,47,2,10,45,66,92], | |
[22,34,60,43,0,72,52], | |
[10,11,53,58] | |
] | |
def sum1(input): | |
return sum((lambda x: (x < 10 and [x*2] or [x/2])[0])(num) | |
for seq in inputdata | |
if len(seq) >= 5 | |
for num in seq | |
if num % 2 == 0) | |
def sum2(input): | |
def getsublist(): | |
for sublist in input: | |
if len(sublist) >= 5: | |
yield sublist | |
def filterdata(sublist): | |
for data in sublist: | |
if data % 2 == 0: | |
yield data | |
def processdata(data): | |
if data < 10: | |
return data * 2 | |
return data / 2 | |
result = 0 | |
for sublist in getsublist(): | |
for data in filterdata(sublist): | |
result += processdata(data) | |
return result | |
def sum3(input): | |
def filterList(matrix): | |
return filter(lambda x: len(x) >= 5, matrix) | |
def filterData(list): | |
return filter(lambda x: x % 2 is 0, list) | |
def processData(data): | |
return data < 10 and data * 2 or data / 2 | |
def reduceList(list): | |
return reduce(lambda x, y: x + processData(y), filterData(list), 0) | |
def reduceMatrix(matrix): | |
return reduce(lambda result, list: result + reduceList(list), filterList(matrix), 0) | |
return reduceMatrix(input) | |
if __name__ == '__main__': | |
print sum1(inputdata) | |
print sum2(inputdata) | |
print sum3(inputdata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/xiachufang/BlackCookbook/blob/master/python/sumit.py