Skip to content

Commit bfcabb4

Browse files
committed
added the backend of the browser history
1 parent 2f765ae commit bfcabb4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

BrowserHistory/backend.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class DLL:
2+
def __init__(self, val: str =None):
3+
self.val = val
4+
self.nxt = None
5+
self.prev = None
6+
7+
8+
class BrowserHistory:
9+
"""
10+
This class designs the operations of a
11+
broswer history
12+
"""
13+
14+
def __init__(self, homepage: str):
15+
self.head = DLL(homepage)
16+
self.curr = self.head
17+
18+
def visit(self, url: str) -> None:
19+
url_node = DLL(url)
20+
self.curr.nxt = url_node
21+
url_node.prev = self.curr
22+
23+
self.curr = url_node
24+
25+
26+
def back(self, steps: int) -> str:
27+
while steps > 0 and self.curr.prev:
28+
self.curr = self.curr.prev
29+
steps -= 1
30+
return self.curr.val
31+
32+
33+
def forward(self, steps: int) -> str:
34+
while steps > 0 and self.curr.nxt:
35+
self.curr = self.curr.nxt
36+
steps -= 1
37+
return self.curr.val
38+
39+
40+
if __name__ == "__main__":
41+
obj = BrowserHistory("google.com")
42+
obj.visit("twitter.com")
43+
param_2 = obj.back(1)
44+
param_3 = obj.forward(1)
45+
46+
print(param_2)
47+
print(param_3)

0 commit comments

Comments
 (0)