From bfcabb4cc1e3a5cd1a14f751d7e0910bf79c1a07 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Thu, 24 Oct 2024 17:05:54 +0100 Subject: [PATCH 1/2] added the backend of the browser history --- BrowserHistory/backend.py | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 BrowserHistory/backend.py diff --git a/BrowserHistory/backend.py b/BrowserHistory/backend.py new file mode 100644 index 00000000000..20ee0005c13 --- /dev/null +++ b/BrowserHistory/backend.py @@ -0,0 +1,47 @@ +class DLL: + def __init__(self, val: str =None): + self.val = val + self.nxt = None + self.prev = None + + +class BrowserHistory: + """ + This class designs the operations of a + broswer history + """ + + def __init__(self, homepage: str): + self.head = DLL(homepage) + self.curr = self.head + + def visit(self, url: str) -> None: + url_node = DLL(url) + self.curr.nxt = url_node + url_node.prev = self.curr + + self.curr = url_node + + + def back(self, steps: int) -> str: + while steps > 0 and self.curr.prev: + self.curr = self.curr.prev + steps -= 1 + return self.curr.val + + + def forward(self, steps: int) -> str: + while steps > 0 and self.curr.nxt: + self.curr = self.curr.nxt + steps -= 1 + return self.curr.val + + +if __name__ == "__main__": + obj = BrowserHistory("google.com") + obj.visit("twitter.com") + param_2 = obj.back(1) + param_3 = obj.forward(1) + + print(param_2) + print(param_3) \ No newline at end of file From fe60f2c95a4e13ea8d3fa9b2d60eebcf9f18bd37 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Thu, 24 Oct 2024 17:57:11 +0100 Subject: [PATCH 2/2] added comments to code --- BrowserHistory/backend.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/BrowserHistory/backend.py b/BrowserHistory/backend.py index 20ee0005c13..5eed004cbf2 100644 --- a/BrowserHistory/backend.py +++ b/BrowserHistory/backend.py @@ -1,4 +1,9 @@ class DLL: + """ + a doubly linked list that holds the current page, + next page, and previous page. + Used to enforce order in operations + """ def __init__(self, val: str =None): self.val = val self.nxt = None @@ -7,15 +12,30 @@ def __init__(self, val: str =None): class BrowserHistory: """ - This class designs the operations of a - broswer history + This class designs the operations of a browser history + + It works by using a doubly linked list to hold the urls """ def __init__(self, homepage: str): + """ + Returns - None + Input - None + ---------- + - Initialize doubly linked list which will serve as the + browser history and sets the current page + """ self.head = DLL(homepage) self.curr = self.head def visit(self, url: str) -> None: + """ + Returns - None + Input - str + ---------- + - Adds the current url to the DLL + - sets both the next and previous values + """ url_node = DLL(url) self.curr.nxt = url_node url_node.prev = self.curr @@ -24,6 +44,13 @@ def visit(self, url: str) -> None: def back(self, steps: int) -> str: + """ + Returns - str + Input - int + ---------- + - Iterates through the DLL backwards `step` number of times + - returns the appropriate value + """ while steps > 0 and self.curr.prev: self.curr = self.curr.prev steps -= 1 @@ -31,6 +58,13 @@ def back(self, steps: int) -> str: def forward(self, steps: int) -> str: + """ + Returns - str + Input - int + ---------- + - Iterates through the DLL forewards `step` number of times + - returns the appropriate value + """ while steps > 0 and self.curr.nxt: self.curr = self.curr.nxt steps -= 1