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

I have a better way to solve exercise 89 #217

Open
anthonyduong9 opened this issue Sep 30, 2024 · 2 comments
Open

I have a better way to solve exercise 89 #217

anthonyduong9 opened this issue Sep 30, 2024 · 2 comments

Comments

@anthonyduong9
Copy link

anthonyduong9 commented Sep 30, 2024

I think

print(np.sort(Z)[-n:])

or

Z.sort()
print(Z[-n:])

would be better than

print (Z[np.argsort(Z)[-n:]])

because the first solution is simpler, and if it's ok to modify the array in place, the second solution is faster and uses less space.

@rougier
Copy link
Owner

rougier commented Oct 21, 2024

It is written that argsort is the slow solution and Z[np.argpartition(-Z,n)[:n]] is faster because of partial ordering.

@anthonyduong9
Copy link
Author

I read that, but either of the solutions I wrote would be a better slow solution, right?

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

2 participants