Drill

ProblemsPython › patterns

Reverse a list end to end

easypatternsTwo pointersArraysPython

A replay view shows the most recent reading first, so the buffer has to come back the other way round.

reverse_readings(readings: list<int>) → list<int>

Solve it in the editor →

Where you start

def reverse_readings(readings: list[int]) -> list[int]:
    

Worked examples

CallResult
reverse_readings([1, 2, 3])[3, 2, 1]
reverse_readings([1, 2, 3, 4])[4, 3, 2, 1]
reverse_readings([7])[7]
reverse_readings([])[]

Hint

Put one index at each end. Swap what they point at, then step them towards each other until they meet.

Reference solution in Python
def reverse_readings(readings: list[int]) -> list[int]:
    out = list(readings)
    i, j = 0, len(out) - 1
    while i < j:
        out[i], out[j] = out[j], out[i]
        i += 1
        j -= 1
    return out

The same problem in another language

More patterns problems in Python