desarrollar un método que admita como parámetro un ÚNICO VALOR
Publicado por Raul Sanchez (3 intervenciones) el 04/04/2020 00:57:02
Hola buenas, necesito desarrollar un método que admita como parámetro un ÚNICO VALOR que se va a insertar en la lista. El valor se insertará en orden después de todos los que sean menores y justo antes de los que sean mayores o iguales. La lista permanecerá ordenada de menor a mayor en todo momento.
Si me podeis echar una mano en este método os lo agradeceria mucho. Gracias anticipadas
Adjunto el código
ESTE PRIMER BLOQUE DE CÓDIGO NO SE PUEDE MODIFICAR
SLT ES EL NOMBRE DEL FICHERO QUE CONTIENE LA CLASE SORTEDLINKEDLIST
ESTE SEGUNDO BLOQUE DE CÓDIGO TAMPOCO SE PUEDE MODIFICAR
A PARTIR DE AQUI ES DONDE VA EL MÉTODO QUE ESTOY INTENTANDO IMPLEMENTAR
Si me podeis echar una mano en este método os lo agradeceria mucho. Gracias anticipadas
Adjunto el código
ESTE PRIMER BLOQUE DE CÓDIGO NO SE PUEDE MODIFICAR
SLT ES EL NOMBRE DEL FICHERO QUE CONTIENE LA CLASE SORTEDLINKEDLIST
1
2
3
4
5
6
7
8
9
10
11
12
import random
from slt import SortedLinkedList
if __name__ == "__main__":
my_list = SortedList()
for i in range(10):
my_list.insert(random.randint(10, 78))
for item in my_list:
print(item)
ESTE SEGUNDO BLOQUE DE CÓDIGO TAMPOCO SE PUEDE MODIFICAR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class SortedLinkedList:
class Node:
def __init__(self, value, next_node = None):
self.value = value
self.next = next_node
def __init__(self):
self.first = None
self.len = 0
def __len__(self):
return self.len
def __iter__(self):
self.current = self.first
return self
def __next__(self):
if self.current != None:
result = self.current.value
self.current = self.current.next_node
return result
else:
raise StopIteration
A PARTIR DE AQUI ES DONDE VA EL MÉTODO QUE ESTOY INTENTANDO IMPLEMENTAR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def insert (self, value):
if value == 0:
self.add_value(value)
elif value == len(self):
self.append(value)
elif value < 0 or value > len(self):
raise IndexError
else:
current = self.__first
current_pos = 1
while current_pos < value:
current = current.next_node
current_pos += 1
current.next_node = self.Node(value, current.next_node)
self.__len += 1
Valora esta pregunta


0