Advertisement
smj007

Untitled

Mar 5th, 2024 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. # 1
  2. class BrowserHistory:
  3.  
  4.     """
  5.    Segregage current, history and future and then this problem will be relatively easy
  6.    """
  7.  
  8.     def __init__(self, homepage: str):
  9.         self.history, self.future = [], []
  10.         self.current = homepage
  11.  
  12.  
  13.     def visit(self, url: str) -> None:
  14.         self.history.append(self.current)
  15.         self.current = url
  16.         self.future = []
  17.        
  18.     def back(self, steps: int) -> str:
  19.        
  20.         while steps and self.history:
  21.             self.future.append(self.current)
  22.             self.current = self.history[-1]
  23.             self.history.pop()
  24.             steps = steps - 1
  25.  
  26.         return self.current
  27.  
  28.     def forward(self, steps: int) -> str:
  29.  
  30.         while steps and self.future:
  31.             self.history.append(self.current)
  32.             self.current = self.future[-1]
  33.             self.future.pop()
  34.             steps = steps - 1
  35.  
  36.         return self.current
  37.  
  38. # 2
  39. class BrowserHistory:
  40.  
  41.     def __init__(self, homepage: str):
  42.  
  43.         self.history = []
  44.         self.future = []
  45.         self.current = homepage
  46.        
  47.     def visit(self, url: str) -> None:
  48.         self.history.append(self.current)
  49.         self.current = url
  50.         self.future = []
  51.        
  52.     def back(self, steps: int) -> str:
  53.  
  54.         count_steps = 0
  55.         while self.history and count_steps < steps:
  56.             count_steps += 1
  57.             recent = self.history.pop()
  58.             self.future.append(self.current)
  59.             self.current = recent
  60.  
  61.         return self.current
  62.        
  63.     def forward(self, steps: int) -> str:
  64.  
  65.         count_steps = 0
  66.         while self.future and count_steps < steps:
  67.             count_steps += 1
  68.             self.history.append(self.current)
  69.             self.current = self.future.pop()
  70.    
  71.         return self.current
  72.  
  73. # Your BrowserHistory object will be instantiated and called as such:
  74. # obj = BrowserHistory(homepage)
  75. # obj.visit(url)
  76. # param_2 = obj.back(steps)
  77. # param_3 = obj.forward(steps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement