Skip to content

Instantly share code, notes, and snippets.

@nenetto
Created September 26, 2023 10:34
Show Gist options
  • Save nenetto/418d1d7d38c456bf78d341f78eaadc21 to your computer and use it in GitHub Desktop.
Save nenetto/418d1d7d38c456bf78d341f78eaadc21 to your computer and use it in GitHub Desktop.
Heap Queue & Bisection
# heaps in standard list types with functions like heappush, heappop, and nsmallest
# Items are always removed by highest priority (lowest number) first.
a = []
heappush(a, 5)
heappush(a, 3)
heappush(a, 7)
heappush(a, 4)
# print(heappop(a), heappop(a), heappop(a), heappop(a))
# >>> 3 4 5 7
# bisect module’s functions, such as bisect_left, provide an efficient binary search
# through a sequence of sorted items. The index it returns is the insertion point
# of the value into the sequence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment