Compare commits

...

2 Commits

Author SHA1 Message Date
perro tuerto fc485834d6 Ejercicios 15a y 15b arreglados 2023-05-24 10:05:04 -07:00
perro tuerto 4e9535e737 Ejercicio 15b 2023-05-24 08:55:53 -07:00
3 changed files with 119 additions and 6 deletions

77
exercises/ex15/queue.py Normal file
View File

@ -0,0 +1,77 @@
class QueueNode(object):
def __init__(self, prev, value, nxt):
self.value = value
self.next = nxt
self.prev = prev
def __repr__(self):
nval = self.next and self.next.value or None
pval = self.prev and self.prev.value or None
return f"[{repr(pval)}, {self.value}, {repr(nval)}]"
class Queue(object):
def __init__(self):
self.begin = None
self.end = None
def count(self):
"""
Returns number of items.
Regresa la cantidad de ítems.
"""
count = 0
node = self.begin
while node:
count += 1
node = node.prev
return count
def unshift(self):
"""
Removes the first item and returns its value.
Remueve el primer ítem y regresa su valor.
"""
if self.begin is None:
value = None
else:
value = self.begin.value
if self.begin.prev is None:
self.begin = self.end = None
else:
self.begin = self.begin.prev
self.begin.next = None
return value
def shift(self, obj):
"""
Appends a new item on the begin of the list.
Añade un nuevo ítem al inicio de la lista.
"""
node = QueueNode(None, obj, self.begin)
if self.begin is None:
self.begin = node
self.end = self.begin
else:
self.begin.prev = self.end = node
def drop(self):
"""
Removes the last item and returns its value.
Remueve el último ítem y regresa su valor.
"""
value = self.end and self.end.value or None
if value:
if self.end.next is None:
self.begin = self.end = None
else:
self.end = self.end.next
self.end.prev = None
return value
def first(self):
"""
Returns value of first item.
Regresa el valor del primer ítem.
"""
return self.begin and self.begin.value or None

View File

@ -0,0 +1,40 @@
from queue import Queue
def test_shift():
colors = Queue()
colors.shift("Pthalo Blue")
assert colors.count() == 1
colors.shift("Ultramarine Blue")
assert colors.count() == 2
def test_unshift():
colors = Queue()
colors.shift("Magenta")
colors.shift("Alizarin")
assert colors.unshift() == "Magenta"
assert colors.unshift() == "Alizarin"
assert colors.unshift() is None
def test_first():
colors = Queue()
colors.shift("Cadmium Red Light")
assert colors.first() == "Cadmium Red Light"
colors.shift("Hansa Yellow")
assert colors.first() == "Cadmium Red Light"
colors.shift("Pthalo Green")
assert colors.first() == "Cadmium Red Light"
def test_drop():
colors = Queue()
colors.shift("Cad Red")
colors.shift("Hansa Yellow")
assert colors.count() == 2
colors.drop()
assert colors.count() == 1
assert colors.first() == "Cad Red"
colors.drop()
assert colors.first() is None

View File

@ -14,9 +14,7 @@ def test_pop():
colors.push("Magenta")
colors.push("Alizarin")
assert colors.pop() == "Alizarin"
assert colors.count() == 1
assert colors.pop() == "Magenta"
assert colors.count() == 0
assert colors.pop() is None
@ -26,7 +24,5 @@ def test_top():
assert colors.top() == "Cadmium Red Light"
colors.push("Hansa Yellow")
assert colors.top() == "Hansa Yellow"
colors.pop()
assert colors.top() == "Cadmium Red Light"
colors.pop()
assert colors.top() is None
colors.push("Pthalo Green")
assert colors.top() == "Pthalo Green"