ASSIGNMENT-4
Aim: Design browser history
Program:
class BrowserHistory:
def init (self, homepage: str):
self.history = [homepage]
self.current = 0
self.max_index = 0 def
visit(self, url: str) -> None:
self.current += 1 if self.current <
len(self.history):
self.history[self.current] = url
else:
self.history.append(url) self.max_index =
self.current def back(self, steps: int) -> str: self.current =
max(0, self.current - steps) return
self.history[self.current] def forward(self, steps: int) ->
str: self.current = min(self.max_index, self.current +
steps) return self.history[self.current]
Output:
1) Aim: LRU Cache Program:
class LRUCache: def init (self,
capacity: int): self.cache =
OrderedDict() self.capacity =
capacity def get(self, key: int) ->
int:
if key in self.cache:
self.cache.move_to_end(key)
return self.cache[key] return -1 def
put(self, key: int, value: int) -> None: if
key in self.cache:
self.cache.move_to_end(key)
self.cache[key] = value if
len(self.cache) > self.capacity:
self.cache.popitem(last=False)
Output: