Skip to content

Instantly share code, notes, and snippets.

@luabida
Created January 10, 2023 22:02
Show Gist options
  • Save luabida/edaf68b3346880bc76b977ea06061b5f to your computer and use it in GitHub Desktop.
Save luabida/edaf68b3346880bc76b977ea06061b5f to your computer and use it in GitHub Desktop.
Bisect: bisect() & insort()
import bisect
import random
# bisect()
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect.bisect(breakpoints, score)
return grades[i]
[grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
# ['F', 'A', 'C', 'C', 'B', 'A', 'A']
# insort()
l = list()
for i in range(10):
randint = random.randrange(50*2 - 1)
bisect.insort(l, randint)
print('%2d ->' % randint, l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment