diff options
| author | root <root@turin.home> | 2022-01-16 13:26:16 +0000 |
|---|---|---|
| committer | root <root@turin.home> | 2022-01-16 13:26:16 +0000 |
| commit | 4f95a0a75be6948a7eb36a8f5563301e740c8d32 (patch) | |
| tree | 9feaac229deb79be4ad59623de1b68b971438ae4 | |
| parent | 0a13b53c5c3de8e884172a2c07c6c7fa3ae72215 (diff) | |
Implement circular queue
| -rw-r--r-- | queue-test.py | 15 | ||||
| -rw-r--r-- | queue.py | 19 |
2 files changed, 34 insertions, 0 deletions
diff --git a/queue-test.py b/queue-test.py new file mode 100644 index 0000000..90838f5 --- /dev/null +++ b/queue-test.py @@ -0,0 +1,15 @@ +from queue import circular_queue + +def test_queue_average(): + a = circular_queue(50) + a.add(1) + a.add(2) + a.add(3) + assert a.average() == 2 + + +def test_queue_average_null(): + a = circular_queue(50) + assert a.average() == 0 + + diff --git a/queue.py b/queue.py new file mode 100644 index 0000000..85516cf --- /dev/null +++ b/queue.py @@ -0,0 +1,19 @@ +class circular_queue: + def __init__(self,size): + self.size = size + self.queue = [] + for i in range(self.size): + self.queue.append(None) + self.pointer = 0 + def add(self, value): + self.queue[self.pointer] = value + self.pointer += 1 + if self.pointer > 49: + self.pointer = 0 + def average(self): + queue = self.queue + while None in queue: + queue.remove(None) + if len(queue) == 0: + return 0 + return sum(queue) / len(queue) |
