Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary search too hard #648

Open
cmcaine opened this issue Jul 11, 2023 · 0 comments
Open

Binary search too hard #648

cmcaine opened this issue Jul 11, 2023 · 0 comments

Comments

@cmcaine
Copy link
Contributor

cmcaine commented Jul 11, 2023

We instruct students to return a range (like searchsorted does), but also we guarantee that there is only zero or one instance of the target value in the array unless the student enables the bonus tests. This is confusing.

IMHO, we should have the student provide their own implementations of searchsortedfirst (returns the index of the first value >= to the target, which is also the insertion index if you want to insert the target value in the array).

We can test the current behaviour (searchsorted-alike) in bonus tests or another exercise.

Students often come up with complicated solutions because they're doing too much work in the loop. We may want to provide a hint about that and/or link to a resource. Here's something I've posted a few times now to students:

Anyway, the key simplifying observation here is to simplify the body of your while loop so that you never return from it. Instead, arrange things so that you can always return low:high after the while loop and have it be correct.

You can read this article for details on how to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant