Ejercicios 15a y 15b arreglados

This commit is contained in:
perro tuerto 2023-05-24 10:05:04 -07:00
parent 4e9535e737
commit fc485834d6
3 changed files with 57 additions and 60 deletions

View File

@ -1,5 +1,5 @@
class QueueNode(object):
def __init__(self, value, prev, nxt):
def __init__(self, prev, value, nxt):
self.value = value
self.next = nxt
self.prev = prev
@ -24,7 +24,7 @@ class Queue(object):
node = self.begin
while node:
count += 1
node = node.next
node = node.prev
return count
def unshift(self):
@ -36,11 +36,11 @@ class Queue(object):
value = None
else:
value = self.begin.value
if self.begin == self.end:
if self.begin.prev is None:
self.begin = self.end = None
else:
self.begin = self.begin.next
self.begin.prev = None
self.begin = self.begin.prev
self.begin.next = None
return value
def shift(self, obj):
@ -48,24 +48,30 @@ class Queue(object):
Appends a new item on the begin of the list.
Añade un nuevo ítem al inicio de la lista.
"""
node = QueueNode(obj, None, self.begin)
node = QueueNode(None, obj, self.begin)
if self.begin is None:
self.begin = node
self.end = self.begin
else:
self.begin.prev = node
self.begin = node
self.begin.prev = self.end = node
def top(self):
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.value
def bottom(self):
"""
Returns value of last item.
Regresa el valor del último ítem.
"""
return self.end.value
return self.begin and self.begin.value or None

View File

@ -1,45 +1,40 @@
from queue import Queue
def test_unshift():
colors = Queue()
colors.shift("Viridian")
colors.shift("Sap Green")
colors.shift("Van Dyke")
assert colors.unshift() == "Van Dyke"
assert colors.unshift() == "Sap Green"
assert colors.unshift() == "Viridian"
assert colors.unshift() is None
assert colors.count() == 0
def test_shift():
colors = Queue()
colors.shift("Cadmium Orange")
colors.shift("Pthalo Blue")
assert colors.count() == 1
colors.shift("Carbazole Violet")
colors.shift("Ultramarine Blue")
assert colors.count() == 2
assert colors.unshift() == "Carbazole Violet"
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.unshift() == "Cadmium Orange"
assert colors.count() == 0
def test_top():
colors = Queue()
colors.shift("Cadmium Red Light")
assert colors.top() == "Cadmium Red Light"
colors.shift("Hansa Yellow")
assert colors.top() == "Hansa Yellow"
colors.shift("Pthalo Green")
assert colors.top() == "Pthalo Green"
def test_bottom():
colors = Queue()
colors.shift("Cadmium Red Light")
assert colors.bottom() == "Cadmium Red Light"
colors.shift("Hansa Yellow")
assert colors.bottom() == "Cadmium Red Light"
colors.shift("Pthalo Green")
assert colors.bottom() == "Cadmium Red Light"
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"