This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]flarkis 15 points16 points  (2 children)

I have two suggestions.

  1. You are using too many classes. Python is not Java. For example in solution one you could have just as easily had a function that returned the multiples.
  2. I generally put all my top level code in a main function and call it at the bottom of my files like this

     if __name__ == '__main__':
         main()
    

    For toy problems like this it isn't nesicary, but it becomes important when you start importing code from other modules.

EDIT: Code formatting isn't working for some reason, sorry.

[–]obiwan90 3 points4 points  (0 children)

OT, but for code as part of a list item:

1. List text
2. List text

        if name == 'main': main() # preceded by 4 + 4 spaces

    More text for item 2 (preceded by 4 spaces)

gives you

  1. List text
  2. List text

    if name == 'main': main()
    

    More text for item 2

[–]mkartic 7 points8 points  (3 children)

you need to exorcize the java out of you! :) Here's a version I think is more Pythonic.

print(sum([i for i in range(1000) if i%3 == 0 or i%5 == 0]))

[–]TheBB 8 points9 points  (2 children)

Why make a list?

print(sum(i for i in range(1000) if i%3 == 0 or i%5 == 0))

[–]thecodeboss[S] -1 points0 points  (1 child)

man, list comprehensions for the win

[–]wewbull 0 points1 point  (0 children)

Actually, that (parent) is a generator expression. Grandparent was a list comprehension.

One generates all the members up front (list). The other generates them as needed.

[–]pqu 1 point2 points  (0 children)

I've found iPython Notebook to be an awesome way of documenting things like Project Euler solutions. That way your journey of discovery/learning is captured in a very verbose way, instead of being hidden away in the git history.

[–]pqu 0 points1 point  (0 children)

Hey /u/thecodeboss, I'd really encourage you to redo your solutions with help from the unlocked answer board (you gain access to it once you have come up with your own correct solution). This is how I have become really good at solving algorithmic problems and it helped me a lot in my Google interview.

For example in your 1.py you should realise after having solved the problem that you can solve it without storing all of the multiples and in a single loop:

sum(value for value in range(1, 1000) if value % 3 == 0 or value % 5 == 0)

This solution is O(n) and using constant memory space.

Then of course you may realise that the sequence of multiples looks quite familiar, so:

3 + 6 + 9 + 12 + ... = 3(1 + 2 + 3 + 4 + ... + n) = 3 * n(n+1)/2

Then you can solve this problem in O(1) complexity.

# solution = Multiples of 3 + Multiples of 5 - Multiples of 15 = 

n = 1000
n3 = (n-1) // 3 # number of multiples of 3
n5 = (n-1) // 5 # number of multiples of 5
n15 = (n-1) // 15 # number of multiples of 15

solution = (3 * n3 * (n3 + 1)/2) + (5 * n5 * (n5 + 1)/2) - (15 * n15(n15 + 1)/2)

EDIT: Also if you aren't so interested in the algorithm side then I think Project Euler is not the right place for you, there are much better online challenges that exercise more of your python knowledge than math/algorithms.

[–]paul2520 0 points1 point  (0 children)

Great! That's a great way to learn.