Created
November 26, 2025 12:30
-
-
Save pschanely/79ca232ed00a0854793e11a05a7d8632 to your computer and use it in GitHub Desktop.
Shared via CrossHair Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[int] | |
| _total: int | |
| def push(self, val: int): | |
| self._values.append(val) | |
| self._total += val | |
| def pop(self) -> int: | |
| ''' pre: len(self._values) > 0 ''' | |
| val = self._values.pop(0) | |
| # Oops. We are forgetting to do something here. | |
| 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