A Fixed-Size Ring Buffer

easy
Unit 9 · State Machines0/3 to milestone

A ring buffer keeps the last N items in a fixed-size list, overwriting the oldest when full: ideal for "last 100 log lines" or a rolling metric window.

Implement a class RingBuffer(capacity) backed by a preallocated list (don't use collections.deque):

  • append(x), len(buf), buf.is_full()
  • indexing with buf[i], where 0 is the oldest item and negative indices count from the newest. Out of range raises IndexError.
  • iteration from oldest to newest, and to_list()
  • mean(): the average of the current items, kept in O(1) per append with a running sum (ValueError if empty)
#data-structures#classes#dunder-methods2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Fixed-Size Ring Buffer · TechGradify