Predict the output
What do the two calls return?
changes = [
(101, "note-1", "edit"),
(102, "note-2", "create"),
(103, "note-1", "delete"),
]
def since(cursor: int) -> tuple[list[str], int]:
newer = [c for c in changes if c[0] > cursor]
next_cursor = newer[-1][0] if newer else cursor
return [f"{c[1]}:{c[2]}" for c in newer], next_cursor
print(since(101))
print(since(103))Hint 1 of 1
Which versions are greater than 101?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A (['note-2:create', 'note-1:delete'], 103) then ([], 103)
Correct. Changes after 101, then nothing new after 103.
The cursor is the id of the last change the client has. since(101) returns everything after it and moves the cursor to the newest id; asking again with 103 finds nothing and leaves the cursor as is.
Why not the others?
- B
(['note-1:edit', 'note-2:create', 'note-1:delete'], 103)then([], 103). Cursor 101 excludes change 101. - C
(['note-2:create'], 102)then([], 103). All newer changes are returned at once. - D
(['note-2:create', 'note-1:delete'], 103)then([], 0). With nothing new the cursor stays the same.