Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python bisect code

Practise finding left and right insertion points, inserting while preserving order, and maintaining a list of records by key.

Intermediate ~8 min read

Challenge 1

Find the insertion point before an equal value or where the target would fit.

python Your answer
from bisect import bisect_left
values = [10, 20, 30, 40]
target = 25
position = 
print(position)

Press Ctrl⌘Enter to check your answer

Challenge 2

Find the position after all existing copies of 4.

python Your answer
from bisect import bisect_right
values = [2, 4, 4, 4, 9]
position = 
print(position)

Press Ctrl⌘Enter to check your answer

Challenge 3

Insert 25 into scores while preserving ascending order.

python Your answer
from bisect import insort
scores = [10, 20, 30, 40]

print(scores)

Press Ctrl⌘Enter to check your answer

Challenge 4

Insert the new record while preserving ascending order by its score.

python Your answer
from bisect import insort
records = [{"score": 10, "name": "Ada"}, {"score": 30, "name": "Lin"}]
new_record = {"score": 20, "name": "Sam"}

print([(record["score"], record["name"]) for record in records])

Press Ctrl⌘Enter to check your answer