Ejercicios 15a y 15b arreglados
This commit is contained in:
parent
4e9535e737
commit
fc485834d6
|
@ -1,5 +1,5 @@
|
||||||
class QueueNode(object):
|
class QueueNode(object):
|
||||||
def __init__(self, value, prev, nxt):
|
def __init__(self, prev, value, nxt):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.next = nxt
|
self.next = nxt
|
||||||
self.prev = prev
|
self.prev = prev
|
||||||
|
@ -24,7 +24,7 @@ class Queue(object):
|
||||||
node = self.begin
|
node = self.begin
|
||||||
while node:
|
while node:
|
||||||
count += 1
|
count += 1
|
||||||
node = node.next
|
node = node.prev
|
||||||
return count
|
return count
|
||||||
|
|
||||||
def unshift(self):
|
def unshift(self):
|
||||||
|
@ -36,11 +36,11 @@ class Queue(object):
|
||||||
value = None
|
value = None
|
||||||
else:
|
else:
|
||||||
value = self.begin.value
|
value = self.begin.value
|
||||||
if self.begin == self.end:
|
if self.begin.prev is None:
|
||||||
self.begin = self.end = None
|
self.begin = self.end = None
|
||||||
else:
|
else:
|
||||||
self.begin = self.begin.next
|
self.begin = self.begin.prev
|
||||||
self.begin.prev = None
|
self.begin.next = None
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def shift(self, obj):
|
def shift(self, obj):
|
||||||
|
@ -48,24 +48,30 @@ class Queue(object):
|
||||||
Appends a new item on the begin of the list.
|
Appends a new item on the begin of the list.
|
||||||
Añade un nuevo ítem al inicio de la lista.
|
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:
|
if self.begin is None:
|
||||||
self.begin = node
|
self.begin = node
|
||||||
self.end = self.begin
|
self.end = self.begin
|
||||||
else:
|
else:
|
||||||
self.begin.prev = node
|
self.begin.prev = self.end = node
|
||||||
self.begin = 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.
|
Returns value of first item.
|
||||||
Regresa el valor del primer ítem.
|
Regresa el valor del primer ítem.
|
||||||
"""
|
"""
|
||||||
return self.begin.value
|
return self.begin and self.begin.value or None
|
||||||
|
|
||||||
def bottom(self):
|
|
||||||
"""
|
|
||||||
Returns value of last item.
|
|
||||||
Regresa el valor del último ítem.
|
|
||||||
"""
|
|
||||||
return self.end.value
|
|
||||||
|
|
|
@ -1,45 +1,40 @@
|
||||||
from queue import Queue
|
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():
|
def test_shift():
|
||||||
colors = Queue()
|
colors = Queue()
|
||||||
colors.shift("Cadmium Orange")
|
colors.shift("Pthalo Blue")
|
||||||
assert colors.count() == 1
|
assert colors.count() == 1
|
||||||
colors.shift("Carbazole Violet")
|
colors.shift("Ultramarine Blue")
|
||||||
assert colors.count() == 2
|
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.count() == 1
|
||||||
assert colors.unshift() == "Cadmium Orange"
|
assert colors.first() == "Cad Red"
|
||||||
assert colors.count() == 0
|
colors.drop()
|
||||||
|
assert colors.first() is None
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
|
@ -14,9 +14,7 @@ def test_pop():
|
||||||
colors.push("Magenta")
|
colors.push("Magenta")
|
||||||
colors.push("Alizarin")
|
colors.push("Alizarin")
|
||||||
assert colors.pop() == "Alizarin"
|
assert colors.pop() == "Alizarin"
|
||||||
assert colors.count() == 1
|
|
||||||
assert colors.pop() == "Magenta"
|
assert colors.pop() == "Magenta"
|
||||||
assert colors.count() == 0
|
|
||||||
assert colors.pop() is None
|
assert colors.pop() is None
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,7 +24,5 @@ def test_top():
|
||||||
assert colors.top() == "Cadmium Red Light"
|
assert colors.top() == "Cadmium Red Light"
|
||||||
colors.push("Hansa Yellow")
|
colors.push("Hansa Yellow")
|
||||||
assert colors.top() == "Hansa Yellow"
|
assert colors.top() == "Hansa Yellow"
|
||||||
colors.pop()
|
colors.push("Pthalo Green")
|
||||||
assert colors.top() == "Cadmium Red Light"
|
assert colors.top() == "Pthalo Green"
|
||||||
colors.pop()
|
|
||||||
assert colors.top() is None
|
|
||||||
|
|
Loading…
Reference in New Issue