4 ms·
Interesting! How do you know your algorithm handles the tricky corner cases? I'd imagine you need to write down all the corner cases, write a formal proof for e
by ekidd 16y ago
Interesting! How do you know your algorithm handles the tricky corner cases? I'd imagine you need to write down all the corner cases, write a formal proof for each, and hope that (1) your proof is correct, and (2) your code actually matches your proof. Could you elaborate on how you do this?
Here are a few corner cases I can think of for word-wrapping:
1) The line is empty.
2) The line is 1 character shorter/1 character longer/exactly the same length as the wrap limit.
3) The first break point for the line falls before/after/on the wrap limit.
4) The first character(s) of the newly-wrapped line are spaces.
I don't think that I could just eyeball a word-wrap algorithm and know that it handles all these cases correctly. I would, at a minimum, need to check each case. But at that point, I'm just as well off writing test cases, and allowing the computer to verify that my algorithm does the right thing for each. Of course, that's not as good as a proof by induction for each desired property, but it's a _lot_ less work, and it's almost always adequate. And I don't need to worry about mistakes in my proof.
Am I misunderstanding something about your approach? How would you handle this?
- xyzzyz 16y agoI do not say I do not test my code. I just see no purpose in implementing code that I hope will work. I need to believe it will work, and if it does not, I am sure that it is an implementation, not an algorithm issue. The first attempt in the article was just silly, and the second was just wrong. I would not waste time writing these, instead I would use this time to figure out the real solution and then I would implement it.