Back to General discussions forum
Rodion's comments on the problem solution are interesting, but I have a basic question.
While I understand the superior performance of the dictionary solution, isn't the "unpleasantness" of the if ladder compared to solutions like the substring match just an aesthetic preference? Large if ladders can be ugly, but their appearance can be improved with formatting or replacement with a select statement.
David, Hi - and thanks for curious question!
As I understand, very long "ladder" or "sequence" of ifs will sacrifice performance, though probably it is hardly noticeable
when we talk about just 6 letters. And of course cunning if-else writer may arrange them in tree-like form (so that in
the worst case control flow only needs Log2(N) comparisons.
In general it is the same with simple select statements (though I suspect some compilers optimize them). Actually it is
also an argument against substring match (especially if more than 6 chars are involved). Dictionaries proudly boast
O(1) (constant) access time, but I suspect the overhead of peeking in the dictionary makes this advantage meek on small
sets.
This question and discussion is definitely worth being added to the notes on the problem at least - hopefully I'll do it soon!