-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from sjakobi/fix-sum-of-multiples
sum-of-multiples: Move to functional solution, add tests
- Loading branch information
Showing
2 changed files
with
32 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
class SumOfMultiples(object): | ||
|
||
def __init__(self, *args): | ||
self.numbers = args if args else [3, 5] | ||
|
||
def to(self, limit): | ||
return sum(n | ||
for n in range(limit) | ||
if self.is_multiple(n)) | ||
|
||
def is_multiple(self, m): | ||
return any(m % n == 0 | ||
for n in self.numbers) | ||
def sum_of_multiples(limit, multiples=None): | ||
if multiples is None: | ||
multiples = [3, 5] | ||
elif multiples[0] == 0: | ||
# multiples of 0 don't change the sum | ||
multiples = multiples[1:] | ||
return sum(value for value in range(limit) | ||
if any(value % multiple == 0 | ||
for multiple in multiples)) |
This file contains 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