Skip to content

Instantly share code, notes, and snippets.

@santhalakshminarayana
Last active September 15, 2019 19:10
Show Gist options
  • Save santhalakshminarayana/50dcb08626b6da786ab3aba134b4b27a to your computer and use it in GitHub Desktop.
Save santhalakshminarayana/50dcb08626b6da786ab3aba134b4b27a to your computer and use it in GitHub Desktop.
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.
class Record_Last_N:
def __init__(self,last_n):
self.ord_list=[]
self.last_n=last_n
def record(self,order_id):
self.ord_list.append(r_id)
if len(self.ord_list) > self.last_n:
self.ord_list.pop(0)
def get_last(self,i):
return self.ord_list[-i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment