forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggilperez.py
104 lines (78 loc) · 2.13 KB
/
ggilperez.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# problem 07 Stacks & Queues
# Stack - LIFO
class Stack:
def __init__(self):
self.items = []
def insert(self, item):
self.items.append(item)
def pop(self):
try:
item = self.items.pop()
except IndexError:
item = None
finally:
return item
my_stack = Stack()
my_stack.insert(1)
my_stack.insert(2)
my_stack.insert(3)
my_stack.insert(4)
print(f"{my_stack.pop() = }")
class Queue:
def __init__(self):
self.items = []
def insert(self, item):
self.items.append(item)
def pop(self):
try:
item = self.items.pop(0)
except IndexError:
item = None
finally:
return item
my_queue = Queue()
my_queue.insert(1)
my_queue.insert(2)
my_queue.insert(3)
my_queue.insert(4)
print(f"{my_queue.pop() = }")
# Extra
def web_browser():
stack = Stack()
while True:
option = input("Enter url or select Next / Previous / Exit: ")
if option.lower() == "exit":
print("Exiting...")
break
elif option.lower() == "previous":
current_url = stack.pop()
previous_url = stack.pop()
stack.insert(previous_url) # Insert to be current next loop
if previous_url is None:
print("Home page")
else:
print(f"URL: {previous_url}")
elif option.lower() == "next":
# due a stack deletes values when it pops, can't go next
pass
else:
stack.insert(option)
print(f"URL added {option}")
web_browser()
def printer():
queue = Queue()
while True:
option = input("Enter document or select Print / Exit: ")
if option.lower() == "exit":
print("Exiting...")
break
elif option.lower() == "print":
document = queue.pop()
if document is None:
print("Empty queue")
else:
print(f"Printing document {document}")
else:
queue.insert(option)
print(f"Document added {option}")
printer()