Skip to content

Instantly share code, notes, and snippets.

@r3
Created May 3, 2012 23:05
Show Gist options
  • Save r3/2590237 to your computer and use it in GitHub Desktop.
Save r3/2590237 to your computer and use it in GitHub Desktop.
def find_multiples(factor, end):
"""Creates a list of multiples less than a given end
value from the given factors.
"""
return range(factor, end, factor)
def collect_and_apply(factors, end=1000, func=None):
"""Merges lists of multiples of the given factors
less than the end value (1000 by default). If
a function is passed as an optional argument,
it will be applied to the result prior to return.
"""
result = set()
try:
for factor in factors:
result.update(set(find_multiples(factor, end)))
# Test to see if a function was passed, if it was, apply
# it to the result, otherwise simply return the result.
if func is None:
return result
else:
return func(result)
except TypeError:
print "Factors argument must be iterable."
if __name__ == '__main__':
# Notice that we can supply the third argument, even if we leave
# out the second (end)? This is because we explicitly state that
# our second argument is the 'func' argument. As such, the default
# 1000 will be used for 'end'
print collect_and_apply((3, 5), func=sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment