forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
81 lines (68 loc) · 2.01 KB
/
backend.py
File metadata and controls
81 lines (68 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
self.prev = None
class BrowserHistory:
"""
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
self.curr = url_node
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
return self.curr.val
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
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)