You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a use case I'm working with at the moment (today's LeetCode.com challenge 🤓) it is necessary to know the index at which an item gets inserted to after calling s.add(item), where s is a SortedList.
However, the add method returns nothing and so I have to call s.bisect_left(item) to get the index and then s.add(item) to perform the update. This is a waste of computation, however.
Currently s.add(item) returns nothing so it would be a backwards compatible change to return the index where the item is added. I had a look at sortedlist.py and it appears that this would be a fairly straight forward feature to implement, but I couldn't go about it myself as I can't wrap my head around a few edge cases e.g. how best to handle when the inserted item already exists and hence the index it enters is arbitrary.
Thanks,
Marrick.
The text was updated successfully, but these errors were encountered:
Getting the index of the item requires the positional index to be built which add() specifically avoids. I think this is a fine example for simply inheriting and implementing the needed functionality in your own code.
The other problem is that bisect.insort doesn’t return the index and that’s what’s used by the library. Get that changed in Python and the project could benefit.
I just asked the same question (sorry for not searching properly).
Did you MarrickLip implement your own function that does this and is still faster than doing both operations one after the other?
Hi,
For a use case I'm working with at the moment (today's LeetCode.com challenge 🤓) it is necessary to know the index at which an item gets inserted to after calling
s.add(item)
, wheres
is aSortedList
.However, the
add
method returns nothing and so I have to calls.bisect_left(item)
to get the index and thens.add(item)
to perform the update. This is a waste of computation, however.Currently
s.add(item)
returns nothing so it would be a backwards compatible change to return the index where the item is added. I had a look atsortedlist.py
and it appears that this would be a fairly straight forward feature to implement, but I couldn't go about it myself as I can't wrap my head around a few edge cases e.g. how best to handle when the inserted item already exists and hence the index it enters is arbitrary.Thanks,
Marrick.
The text was updated successfully, but these errors were encountered: