Skip to content

Instantly share code, notes, and snippets.

@pschanely
Created March 19, 2026 20:35
Show Gist options
  • Select an option

  • Save pschanely/ed11ad61f1736b1d48928420d3f8bdb6 to your computer and use it in GitHub Desktop.

Select an option

Save pschanely/ed11ad61f1736b1d48928420d3f8bdb6 to your computer and use it in GitHub Desktop.
Shared via CrossHair Playground
import dataclasses
from typing import List
@dataclasses.dataclass
class AverageableQueue:
'''
A queue of numbers with a O(1) average() operation.
inv: self._total == sum(self._values)
'''
_values: List[float]
_total: float
def push(self, val: float):
'''
pre: not math.isnan(val)
'''
self._values.append(val)
self._total += val
def pop(self) -> float:
''' pre: len(self._values) > 0 '''
val = self._values.pop(0)
# Oops. We are forgetting to do something here.
self._total -= val
return val
def average(self) -> float:
''' pre: self._values '''
return self._total / len(self._values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment