-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_record.py
30 lines (21 loc) · 865 Bytes
/
log_record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Coding Problem #16
You run an e-commerce website and want to record the last N order ids in a log.
Implement a data structure to accomplish this, with the following API:
-> record(order_id): adds the order_id to the log
-> get_last(i): gets the ith last element from the log.
i is guaranteed to be smaller than or equal to N.
You should be as efficient with time and space as possible.
"""
from linked_list import DoublyLinkedList
class Log(DoublyLinkedList):
def __init__(self) -> DoublyLinkedList:
super().__init__()
def record(self, order_id: int) -> None:
self.append(order_id)
def get_last(self, i: int) -> int:
if i > self.length:
raise LookupError(f'Only {self.length} logs recorded')
node = self.tail
for _ in range(1,i):
node = node.prev
return node.value