Looks pretty good to me. Personally, I would get rid of the
found variable and just
return True if
item is found.
I would also use a more descriptive name for the list.
a just doesn't say list to me. If you want to keep it short, use something like
l or borrow Haskell's convention and use something like
Finally, if you wanted, you could use an
else instead of the
else with nested
else inside. But I can't think of any compelling reason to do so.
As an answer to an interview question, I think you'll want to do a better job of indicating coverage of the case where the list is empty. Your code covers this case perfectly (
last will be
first will be
first <= last is
False), but it's not obvious. So it probably wouldn't hurt to add an explicit check or add a comment with a brief explanation why that case is covered. Then there's no doubt that you considered that case.