- 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