No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

# Playground - Implementación de una singly linked list

51/56

Aportes 18

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

Fue una buena practica

#### Archivo Node

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````

#### Archivo main

``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):

self.tail = None
self.length = 0
self.max_beds = max_beds

if self.getAvailableBeds() == 0:
raise Exception("No hay camas disponibles")

beds = [patient['bedNumber'] for patient in self.getPatientList()]

empty_bed = [bed for bed in range(
1, self.max_beds+1) if not bed in beds]

new_node = PatientNode(name, age, empty_bed[0])

self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1

def removePatient(self, name):

return
self.length -= 1
return
while current_node.next:
if current_node.next.name == name:
current_node.next = current_node.next.next
self.length -= 1
return
current_node = current_node.next

def getPatient(self, name):

if current_node:
while current_node:
if current_node.name == name:
return {
'name': current_node.name,
'age': current_node.age,
'bedNumber': current_node.bed_number
}
current_node = current_node.next

def getPatientList(self):

patient_list = []

if current_node:
while current_node:
patient_list.append(
self.getPatient(current_node.name)
)
current_node = current_node.next
return patient_list

def getAvailableBeds(self):

return self.max_beds - self.length
``````

No lo decian en la guia, pero la lista debe crecer por la cola.
Eso me hizo rehacer mi rutina de addPatient

o
o
o
o
o
o
o
o

``````class PatientNode:
def __init__(self, name, age, bedNumber):
self.data = {}
self.data["name"]       = name
self.data["age"]        = age
self.data["bedNumber"] = bedNumber
self.next = None
``````
``````class PatientList:
def __init__(self, max_beds):
self.camas_disponibles = [x + 1 for x in range(max_beds)]
self.cabeza = None

if len(self.camas_disponibles) > 0:
newPatient = PatientNode(name, age, self.camas_disponibles.pop(0))
if self.cabeza == None:
self.cabeza = newPatient
else:
anterior.next = newPatient
else:
raise Exception("No hay camas disponibles")

def removePatient(self, name):
else:

def getPatient(self, name):
salida = None
else:

return salida
else:

def getPatientList(self):
salida= []

return salida

def getAvailableBeds(self):
return len(self.camas_disponibles)

def getListAvailableBeds(self):
return self.camas_disponibles
``````
``````pacientes = PatientList(8)

print(pacientes.getPatientList())
print(pacientes.getAvailableBeds())
print(pacientes.getListAvailableBeds())

pacientes.removePatient("Wonderwoman")
print(pacientes.getPatientList())
print(pacientes.getAvailableBeds())
print(pacientes.getListAvailableBeds())

print(pacientes.getPatientList())
print(pacientes.getAvailableBeds())
print(pacientes.getListAvailableBeds())

print(pacientes.getPatient("Spiderman"))

``````

#### 🛡️🛡️Escudo anti-spoilers🛡️🛡️

Mi solución al reto:

main

``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.tail = None
self.length = 0
self.max_beds = max_beds
self.free_beds = []
print("Se ha creado la lista con un maximo de camas de ",self.max_beds)

if self.length >= self.max_beds:
raise Exception("No hay camas disponibles")

new_node = PatientNode(name, age, self.length + 1)

self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node

self.length += 1

def __free_bed(self, bed_number):
self.free_beds.append(bed_number)
self.length -= 1

def removePatient(self, name):

return

prev = None
while current:
if current.name == name:
self.__free_bed(current.bed_number)
prev.next = current.next
return
prev = current
current = current.next

def getPatient(self, name):
if self.length <= 0:
raise Exception("No hay pacientes en la lista")

while current_node:
if current_node.name == name:
return {
"name": current_node.name,
"age": current_node.age,
"bedNumber": current_node.bed_number
}
current_node = current_node.next

def getPatientList(self):
r = []
while current_node:
r.append({
"name": current_node.name,
"age": current_node.age,
"bedNumber": current_node.bed_number
})
current_node = current_node.next
return r

def getAvailableBeds(self):
return self.max_beds - self.length
``````

node

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````

SPOILER ALERRT
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

Mi código:

``````#  Node.py
class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````
``````#  main.py
from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.max_beds = max_beds
self.available_beds = max_beds

if self.available_beds == 0:
raise Exception("No hay camas disponibles")

new_bed_number = self.max_beds - self.available_beds + 1
new_patient = PatientNode(name, age, new_bed_number)

else:
while current.next:
current = current.next
current.next = new_patient

self.available_beds -= 1

def removePatient(self, name):

self.available_beds += 1
return

prev = None
while current and current.name != name:
prev = current
current = current.next

if current:
self.available_beds += 1
prev.next = current.next
else:

def getPatient(self, name):
while current:
if current.name == name:
return {'name': current.name, 'age': current.age, 'bedNumber': current.bed_number}
current = current.next

def getPatientList(self):
patient_list = []
while current:
patient_list.append({'name': current.name, 'age': current.age, 'bedNumber': current.bed_number})
current = current.next
return patient_list

def getAvailableBeds(self):
return self.available_beds

``````

.
.
.
.
.
.
.
.
.
.

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
def get(self):
return {"name":self.name, "age":self.age, "bedNumber":self.bed_number}

``````

``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.max_beds = max_beds
self.tail = None
self.size = 0

if self.size == self.max_beds:
raise Exception("No hay camas disponibles")
newNode = PatientNode(name, age, self.size + 1)
self.tail = newNode
else:
self.tail.next = newNode
self.tail = newNode
self.size += 1

def removePatient(self, name):
return False
self.size -= 1
return True
while current_node.next.name != name and current_node.next != None:
current_node = current_node.next
if current_node.next != None:
current_node.next = current_node.next.next
self.size -= 1
return True
else:

def getPatient(self, name):
return False
while current_node.next.name != name and current_node.next != None:
current_node = current_node.next
if current_node.next != None:
return current_node.next.get()
else:

def getPatientList(self):
patient_list = []
while current_node != None:
patient_list.append(current_node.get())
current_node = current_node.next
return patient_list

def getAvailableBeds(self):
return self.max_beds - self.size
``````
``````class PatientNode:
def __init__(self, name, age, bed):
self.name = name
self.age = age
self.bed = bed
self.next = None

def retorno(self):
return {'name': self.name, 'age': self.age, 'bedNumber': self.bed}

class PatientList:

def __init__(self, max_beds):
self.tail = None
self.length = 0
self.max_beds = max_beds
self.beds = {bed: True for bed in range(1, max_beds + 1)}

def bed_available(self):
bed = next((bed for bed in self.beds if self.beds[bed]), None)
self.beds[bed] = False
if bed is None:
raise Exception("No hay camas disponibles")
return bed

bed = self.bed_available()
new_node = PatientNode(name, age, bed)
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1

def removePatient(self, name):
return
self.length -= 1
return
while current_node.next:
if current_node.next.name == name:
self.beds[current_node.next.bed] = True
current_node.next = current_node.next.next
self.length -= 1
return
current_node = current_node.next

def getPatient(self, name):
while current_node:
if current_node.name == name:
return current_node.retorno()
current_node = current_node.next
if current_node is None:

def getPatientList(self):
patiens = []
while current_node:
patiens.append(current_node.retorno())
current_node = current_node.next
return patiens

def getAvailableBeds(self):
return self.max_beds - self.length
``````

Estuvo divertido el reto, espero que lo encuentren útil. Aquí mi solución.

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````
``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.tail =None
self.length = 0
self.max_beds=max_beds
self.beds={bed+1:False for bed in range(self.max_beds)}
self.available_beds=max_beds

def find_next_available_bed(self, beds, condition):
for bed, availability in beds.items():
if condition(availability):
return bed

if self.available_beds==0:
raise Exception("No hay camas disponibles")
else:
next_available_bed=self.find_next_available_bed(self.beds,lambda b:b is False)
new_node = PatientNode(name, age, next_available_bed)
self.beds[next_available_bed]=True
self.available_beds-=1
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1

def removePatient(self, name):
return
patient_found=False
patient_found=True
self.available_beds+=1
self.length -= 1
return
while current_node.next:
if current_node.next.name == name:
patient_found=True
self.beds[current_node.next.bed_number]=False
self.available_beds+=1
current_node.next = current_node.next.next
self.length -= 1
return
current_node = current_node.next
if not patient_found:

def getPatient(self, name):
return
patient_found=False
patient_found=True
while current_node.next:
if current_node.next.name == name:
patient_found=True
return {"name":current_node.next.name,
"age":current_node.next.age,
"bedNumber":current_node.next.bed_number}
current_node = current_node.next
if not patient_found:

def getPatientList(self):
return
patientList = []
while current_node.next:
patientList.append({"name":current_node.next.name,
"age":current_node.next.age,
"bedNumber": current_node.next.bed_number})
current_node = current_node.next
return patientList

def getAvailableBeds(self):
return self.available_beds

lista = PatientList(3)

print(lista.getPatientList())
``````

Solución 😄…
.

.
.
.
.
Node:

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````

Main:

``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.tail = None
self.length = 0
self.max_beds = max_beds
self.beds = [
True
for _ in range(max_beds)
]

if self.length == self.max_beds:
raise Exception("No hay camas disponibles")
bed_number = self.getAvailableBedsList()[0]
self.beds[bed_number - 1] = False
newPatient = PatientNode(name, age, bed_number)
self.tail = newPatient
else:
self.tail.next = newPatient
self.tail = newPatient
self.length += 1

def removePatient(self, name):
self.length -= 1
return
while actual.next:
if actual.next.name == name:
self.beds[actual.next.bed_number - 1] = True
actual.next = actual.next.next
self.length -= 1
return
actual = actual.next

def getPatient(self, name):
while actual:
if actual.name == name:
return {
'name': actual.name,
'age': actual.age,
'bedNumber': actual.bed_number
}
actual = actual.next

def getPatientList(self):
patients = []
while actual:
patients.append({
'name': actual.name,
'age': actual.age,
'bedNumber': actual.bed_number
})
actual = actual.next
return patients

def getAvailableBeds(self):
return self.max_beds - self.length

def getAvailableBedsList(self):
return [
index + 1
for index, bed in enumerate(self.beds)
if bed
]
``````

### Mi solución 💻 🐍

.
.
.
.

#### `Node.py`

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
``````

#### `main.py`

``````class PatientList:
def __init__(self, max_beds):
self.beds = max_beds
self.occupied_beds = 0

if self.occupied_beds == self.beds:
raise Exception("No hay camas disponibles")

self.occupied_beds += 1
patient = PatientNode(name, age, self.occupied_beds)

else:
while current.next != None:
current = current.next
current.next = patient

def removePatient(self, name):

deleted_patient = False
if current.name == name:
deleted_patient = True
self.occupied_beds -= 1
else:
prev = None
while current:
if current.name == name:
deleted_patient = True
self.occupied_beds -= 1
prev.next = current.next
break
prev = current
current = current.next

if not deleted_patient:

def getPatient(self, name):

while current:
if current.name == name:
return {
"name": current.name,
"age": current.age,
"bedNumber": current.bed_number
}

current = current.next

def getPatientList(self):
patients = []
while current:
patients.append({
"name": current.name,
"age": current.age,
"bedNumber": current.bed_number
})
current = current.next

return patients

def getAvailableBeds(self):
return self.beds - self.occupied_beds
``````
``````test_add_patient_to_list

test_decrement_beds_available

test_throw_error_if_no_beds_available

test_remove_patient_from_list

test_increment_beds_available

test_throw_error_if_patient_not_found

test_return_patient_object

test_return_array_of_patient_objects

``````

Mi solucion:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

``````class PatientNode:
def __init__(self, name, age, bed_number):
# Tu código aquí 👇
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None
from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
# Tu código aquí 👇
self.max_beds = max_beds
self.occupied_beds = 0
self.available_beds = {i: True for i in range(1, self.max_beds+1)}
self.tail = None

# Tu código aquí 👇
if self.occupied_beds == self.max_beds:
raise Exception('No hay camas disponible')
for i in range(1, self.max_beds+1):
if self.available_beds[i] == True:
new_node = PatientNode(name, age, i)
self.occupied_beds +=1
self.available_beds[i] = False
break
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node

def removePatient(self, name):
# Tu código aquí 👇
return
self.occupied_beds -=1
return
while current_node.next:
if current_node.next.name == name:
self.available_beds[current_node.next.bed_number] = True
self.occupied_beds -=1
current_node.next = current_node.next.next
return
current_node = current_node.next

def getPatient(self, name):
# Tu código aquí 👇
return
while current_node:
if current_node.name == name:
return {
'name': current_node.name,
'age': current_node.age,
'bedNumber': current_node.bed_number
}
current_node =current_node.next

def getPatientList(self):
# Tu código aquí 👇
return []
patients = []
while current_node:
patients.append(self.getPatient(current_node.name))
current_node = current_node.next
return patients

def getAvailableBeds(self):
# Tu código aquí 👇
return self.max_beds - self.occupied_beds

``````

.
.
.
.
.

``````class PatientNode:
def __init__(self, name, age, bed_number):
self.name = name
self.age = age
self.bed_number = bed_number
self.next = None

from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
self.max_beds = max_beds
self.bed_count = 0

if self.bed_count >= self.max_beds:
raise Exception("No hay camas disponibles")

self.bed_count+=1
new_patient = PatientNode(name,age,self.bed_count)

else:
while current.next:
current = current.next
current.next = new_patient

def removePatient(self, name):

self.bed_count -= 1
return

prev = None
while current:
if current.name == name:
prev.next = current.next
self.bed_count -= 1
return
prev = current
current = current.next

def getPatient(self, name):
while current:
if current.name == name:
return {
'name': current.name,
'age': current.age,
'bedNumber': current.bed_number
}
current = current.next

def getPatientList(self):
patient_list = []
while current:
patient_list.append({
'name': current.name,
'age': current.age,
'bedNumber': current.bed_number
})
current = current.next
return patient_list

def getAvailableBeds(self):
return self.max_beds - self.bed_count

``````

SPOILER
.
.
.
.

.
.
.
.
.
.
.
.
.
.

``````from Node import PatientNode

class PatientList:
def __init__(self, max_beds):
# Tu código aquí 👇
self.max_beds = max_beds
self.enabled_beds = max_beds
self.primera = None
pass

# Tu código aquí 👇
if self.enabled_beds == 0:
raise Exception("No hay camas disponibles")

if self.primera is None:
self.primera = PatientNode(name, age, 1)
else:
node = self.primera

while not node.next_patient is None:
node = node.next_patient

node.next_patient = PatientNode(name, age, node.bed_number+1)

self.enabled_beds = self.enabled_beds - 1
pass

def removePatient(self, name):
# Tu código aquí 👇
if self.primera.name is name:
self.primera = self.primera.next_patient
return

node = self.primera
while not node.next_patient.name is name:
node = node.next_patient

node.next_patient = node.next_patient.next_patient
self.enabled_beds = self.enabled_beds + 1

pass

def getPatient(self, name):
# Tu código aquí 👇
node = self.primera
while not node is None and not node.name is name:
node = node.next_patient

if node is None:
return {
"name": node.name,
"age": node.age,
"bedNumber": node.bed_number
}

pass

def getPatientList(self):
# Tu código aquí 👇
patientList = []
node = self.primera
while not node.next_patient is None:
patientList.append({
"name": node.name,
"age": node.age,
"bedNumber": node.bed_number
})
node = node.next_patient

patientList.append({
"name": node.name,
"age": node.age,
"bedNumber": node.bed_number
})

return patientList
pass

def getAvailableBeds(self):
# Tu código aquí 👇
return self.enabled_beds
pass

list = PatientList(3)

print(list.getPatientList())

``````
``````class PatientNode:
def __init__(self, name, age, bed_number):
# Tu código aquí 👇
self.name = name
self.age = age
self.bed_number = bed_number
self.next_patient = None
pass
``````

.
.
.
.
.
No pude con uno de los test

undefined