您的位置:首页 > 编程语言 > Python开发

python pieces

2016-04-21 23:08 405 查看
create partial sum of sequence using generator. ref: Blckknght’s answer

def partial_sums(iterable):
total = 0
for i in iterable:
total += i
yield total


None and 0, ”, etc, as condition.

example, to solve two sum problem. below code

def twoSum(self, nums, target):
scanned = {}
for i in range(len(nums)):
# wrong if missing '!=None' in below line, wrong case,
# nums, target = [0,4,3,0], 0
if scanned.get(nums[i])!=None:
return scanned[nums[i]], i
scanned[target - nums[i]] = i
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python