Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1
- class BrowserHistory:
- """
- Segregage current, history and future and then this problem will be relatively easy
- """
- def __init__(self, homepage: str):
- self.history, self.future = [], []
- self.current = homepage
- def visit(self, url: str) -> None:
- self.history.append(self.current)
- self.current = url
- self.future = []
- def back(self, steps: int) -> str:
- while steps and self.history:
- self.future.append(self.current)
- self.current = self.history[-1]
- self.history.pop()
- steps = steps - 1
- return self.current
- def forward(self, steps: int) -> str:
- while steps and self.future:
- self.history.append(self.current)
- self.current = self.future[-1]
- self.future.pop()
- steps = steps - 1
- return self.current
- # 2
- class BrowserHistory:
- def __init__(self, homepage: str):
- self.history = []
- self.future = []
- self.current = homepage
- def visit(self, url: str) -> None:
- self.history.append(self.current)
- self.current = url
- self.future = []
- def back(self, steps: int) -> str:
- count_steps = 0
- while self.history and count_steps < steps:
- count_steps += 1
- recent = self.history.pop()
- self.future.append(self.current)
- self.current = recent
- return self.current
- def forward(self, steps: int) -> str:
- count_steps = 0
- while self.future and count_steps < steps:
- count_steps += 1
- self.history.append(self.current)
- self.current = self.future.pop()
- return self.current
- # Your BrowserHistory object will be instantiated and called as such:
- # obj = BrowserHistory(homepage)
- # obj.visit(url)
- # param_2 = obj.back(steps)
- # param_3 = obj.forward(steps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement