Last active
September 15, 2019 19:10
-
-
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.
This file contains 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
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