1
When running the following code, the variable _order_stability
never gets a False value, even when I try to force it during debug.
The variable can take other values, for example of the type int
other than 0 and 1.
When I run the code, the first line of the function insert
class RandomAccessLinkedList
tries to change the value of the variable unsuccessfully.
I’m using Python 3.5.2 (default) on linux, but the error happens also in windows.
from collections import Sequence
class Node:
def __init__(self, value):
self.value = value
self._next = None
@property
def next(self):
return self._next
@next.setter
def next(self, next_node):
self._next = next_node
def __str__(self):
return repr(self.value)
def __repr__(self):
return str(self.value)
class RandomAccessLinkedList(Sequence):
def __init__(self, *valores):
self._head = Node(valores[0])
self._tail = Node(valores[0])
self._order_stability = True
self._change_point = None
nodo_previous = self._head
for value in valores[1::]:
nodo = Node(value)
nodo_previous.next = nodo
self._tail = nodo
nodo_previous = nodo
self._data = [Node(item) for item in valores]
@property
def data(self):
if self._order_stability:
return self._data
counter = len(self._data)
start = 0
nodo_atual = self._head
if self._change_point is not None:
start = self._change_point
nodo_atual = self._data[start]
self._change_point = None
for i, j in enumerate(range(start, counter)):
self._data[i] = nodo_atual
nodo_atual = nodo_atual.next
self._order_stability = True
return self._data
def next(self):
pass
def insert(self, new_value, index):
self._order_stability = False
self._change_point = index
self._data.append(None)
new_value = Node(new_value)
if index > 0:
previous = self._data[index-1]
pivo = previous.next
previous.next = new_value
new_value.next = pivo
elif index == 0:
new_value.next = self._data[0]
self._head = new_value
elif index == len(self._data)-2:
self._order_stability = True
self._change_point = None
self._data[-1] = new_value
def __getitem__(self, index):
return self.data[index].value
def __len__(self):
return len(self.data)
def __repr__(self):
return repr(self.data)
def __str__(self):
string = 'Ø → {}'
string += ' → {}' * (len(self.data) - 1)
string += ' → Ø'
return string.format(*[i.value for i in self.data])
def __eq__(self, outra_lista):
if id(self) == id(outra_lista):
return True
if len(outra_lista) != len(self):
return False
for i, j in enumerate(outra_lista):
if j != self[i]:
return False
return True
if __name__ == '__main__':
lista = RandomAccessLinkedList(*[i for i in range(5)])
lista.insert(5, 0)
Where is my mistake?
Friend I believe your error is in the declaration of the date property, right in the first line you check if the _order_stability variable is True and returns the self. _data, but note that soon at the initialization of your class you already initialize the variable with True. There might be something wrong with this part. Another thing is that it may also be generating some infinite loops in function statements Len(self) which is returning the length of the property and not the variable, check the references to self.data and self. _data.
– Diego Garcia
Remember that access to self.data triggers the getter function defined in the date and self property. _data accesses the internal variable. I suggest reviewing the uses of the variable.
– Diego Garcia
The problem is that Insert cannot change the value from True to False in the _order_stability variable. Neither forcing the True value during debug changes the variable.
– Arthur Julião
Change it changes, see https://repl.it/EFIT/1
– stderr
Maybe the problem is in the IDE so... thanks @stderr
– Arthur Julião
Actually, the error occurs in the IDE, and only during debug.
– Arthur Julião