3 ms·
Indeed, in the first example the special cases should really be moved out of the loop too, so you have the code looking like this: - code for left edge
by userbinator 8y ago
Indeed, in the first example the special cases should really be moved out of the loop too, so you have the code looking like this:
- code for left edge
- code for body
- code for right edge
Now, both you and the machine don't have to keep thinking about the edge cases every time through the loop body. It's a win-win situation.
- elephantum 8y agoIt means you have two extra special cases: 1) list is empty; 2) left edge is the right edge (I.e. length is 1)
- riskable 8y agoNothing that can't be solved with more conditionals! Conditionals are great because they're clear and concise... if not list: # Can't do anything with this return # or raise error if len(list) < 3: # Ditto return #ditto Sure, there's more code but it's not bad code. It's just straightforward stuff that's easy to read and debug (if necessary).
- deleted 8y ago[deleted]