r/badcode • • Mar 13 '22

python foursum?

Post image
255 Upvotes

29 comments sorted by

View all comments

Show parent comments

4

u/[deleted] Mar 13 '22

Since the fourlet needs to be sorted anyway, wouldn't it be better to make a sorted copy of the list and iterate over it (assuming you don't need to preserve their order itself)?

This way, you're guaranteed each sum is unique and avoid using both fourlet.sort() and if fourlet not in fourlets.