On Sat, Aug 08, 2020 at 01:46:28AM +1000, Chris Angelico wrote: > On Sat, Aug 8, 2020 at 1:38 AM Python <pyt...@bladeshadow.org> wrote: > TBH most people won't get the recursive version right the first time > either.
So FWIW, I/my team don't find this to be true. I was originally going to mention this in my previous post but decided to leave it out for brevity (and in part because I assumed anyone familiar with the problem already expected it was so). In fact my team frequently uses Fibonacci as an interview question. We've been doing so for at least a decade, so we do have a not-insubstantial sample size. We find that candidates can much more easily implement the recursive version correctly, and do so on the first try roughly 70-80% of the time. Nearly all candidates choose to implement the recursive version for that reason. Those that don't are pretty familiar with the problem, and choose the iterative version so they can "show off" their knowledge of the performance benefits. They typically also implement the recursive version immediately after, because it's pretty trivial to do it in most programming languages, and because they assume the point of the question is to ascertain whether they grok recursion. Which is partly true, but only partly--we use it because it is a short problem that can be quickly implemented, and demonstrates a few different interesting concepts, one of which is recursion. When we do use it, we always follow up with "Can you implement this iteratively?" (assuming they didn't already). A small percentage insist that it is impossible. Most who try (again, roughly 70-80%) fail on the first try. Figuring out how to correctly prime the state is pretty much invariably the sticking point. These are not students, they are experienced programmers. This may explain why they usually can easily implement the recursive version, but it does not really address why most of them have trouble with the iterative version. :) -- https://mail.python.org/mailman/listinfo/python-list