Editor

The Last N Items

#556 · Lists · Beginner · Review the AI's code · 30 XP · about 4 min

Someone asked an AI assistant: "Write a function that returns the last n items of a list." This is its answer. It looks right, but it has a bug.

last_n(items, n) should return a new list with the last n items of items, in their original order. When n is 0 the answer is an empty list, and when n is bigger than the list the whole list comes back. n is never negative.

Examples

last_n([1, 2, 3, 4, 5], 2)  # returns [4, 5]
last_n(['a', 'b'], 5)       # returns ['a', 'b']
last_n([1, 2, 3], 0)        # returns []

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.