Playground - Implementación de un stack
Clase 53 de 56 • 21 Días de Python
Contenido del curso
Clase 53 de 56 • 21 Días de Python
Contenido del curso
Hanzel Eliud Lares Valdéz
Juan Diego
Diego Cruz
Salomon Chambi
Victor Ortiz
Alejandro Anaya
Waldir Zapata Garcia
Javier Martínez González
José Miguel Reyes
Gabriel Andrés Montiel Hermosa
iecgerman .
Gonzalo Gutiérrez Castillo
Brayan Estiben Rodallega Saavedra
Harold Zurita Simon
Paola Alapizco
Jhon Freddy Tavera Blandon
Camilo Andrés Rodriguez Higuera
David Hereira
Julio Cardenas
Edgar Alarcón
from node import Node class Playlist: def __init__(self): # Tu código aquí 👇 self.top = None self.bottom = None self.length = 0 def addSong(self, song): # Tu código aquí 👇 newSong = Node(song) if self.length == 0: self.top = newSong self.bottom = newSong else: newSong.next = self.top self.top = newSong self.length +=1 def playSong(self): # Tu código aquí 👇 if self.top is None: raise Exception("No hay canciones en la lista") if self.top == self.bottom: self.bottom = None play = self.top self.top = play.next self.length -= 1 return play.value def getPlaylist(self): # Tu código aquí 👇 list = [] if self.top is None: return list current = self.top while current: list.append(current.value) current = current.next return list
class Node: def __init__(self, value): # Tu código aquí 👇 self.value = value self.next = None pass
class Node: def __init__(self, value): self.value = value self.next = None class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_node = Node(song) if self.length == 0: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length += 1 def playSong(self): if not self.top: raise Exception( "No hay canciones en la lista") if self.top == self.bottom: self.bottom = None popped_node = self.top self.top = self.top.next self.length -=1 return popped_node.value def getPlaylist(self): current = self.top playlist = [] while current: playlist.append(current.value) current = current.next return playlist ```class Node: def \_\_init\_\_(self, value): self.value = value self.next = None class Playlist: def \_\_init\_\_(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new\_node = Node(song) if self.length == 0: self.top = new\_node self.bottom = new\_node else: new\_node.next = self.top self.top = new\_node self.length += 1   def playSong(self): if not self.top: raise Exception( "No hay canciones en la lista") if self.top == self.bottom: self.bottom = None popped\_node = self.top self.top = self.top.next self.length -=1 return popped\_node.value def getPlaylist(self): current = self.top playlist = \[] while current: playlist.append(current.value) current = current.next return playlist
class Node: def __init__(self, value): self.value = value self.next = None class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_node = Node(song) if self.length == 0: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length += 1 def playSong(self): if self.length == 0: raise Exception("No hay canciones en la lista") song = self.top.value self.top = self.top.next self.length -= 1 if self.length == 0: self.bottom = None return song def getPlaylist(self): current = self.top result = [] while current: result.append(current.value) current = current.next return result ```class Node: def \_\_init\_\_(self, value): self.value = value self.next = None class Playlist: def \_\_init\_\_(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new\_node = Node(song) if self.length == 0: self.top = new\_node self.bottom = new\_node else: new\_node.next = self.top self.top = new\_node self.length += 1 def playSong(self): if self.length == 0: raise Exception("No hay canciones en la lista") song = self.top.value self.top = self.top.next self.length -= 1 if self.length == 0: self.bottom = None return song def getPlaylist(self): current = self.top result = \[] while current: result.append(current.value) current = current.next return result
Mi aporte
class Node: def __init__(self, value): self.value = value self.next = None class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_song = Node(song) if self.length == 0: self.top = new_song self.bottom = new_song else: new_song.next = self.top self.top = new_song self.length += 1 def playSong(self): if self.top is None: raise Exception('No hay canciones en la lista') if self.top == self.bottom: self.bottom = None top_song = self.top self.top = self.top.next self.length -= 1 return top_song.value def getPlaylist(self): current = self.top playlist = [] while current is not None: playlist.append(current.value) current = current.next return playlist
Mi aporte: . . . . . . . . . . . . . . . . . . . . .
# node.py class Node: def __init__(self, value): # Tu código aquí 👇 self.value = value self.next = None pass
# main.py from node import Node class Playlist: def __init__(self): # Tu código aquí 👇 self.top = None self.bottom = None self.length = 0 pass def addSong(self, song): # Tu código aquí 👇 newSong = Node(song) if self.length == 0: self.top = newSong self.bottom = newSong else: newSong.next = self.top self.top = newSong self.length += 1 return self pass def playSong(self): # Tu código aquí 👇 if not self.top: raise Exception('No hay canciones en la lista') # return None if self.top == self.bottom: self.bottom = None song = self.top.value self.top = self.top.next self.length -= 1 return song pass def getPlaylist(self): # Tu código aquí 👇 lSong = self.top listSongs = [] while lSong: listSongs.append(lSong.value) lSong = lSong.next return listSongs pass
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
#main.py from node import Node class Playlist: def __init__(self): self.top = self.bottom = None self.length = 0 def addSong(self, song): newSong = Node(song) if self.top is None and self.bottom is None: print(f"ℹ️ Agregando la primera cancion (🎵 {song})") self.top = newSong self.bottom = newSong else: print(f"ℹ️ Agregando una cancion a la playlist (🎵 {song})") lastTop = self.top self.top = newSong self.top.next = lastTop self.length += 1 def playSong(self): if self.length <= 0: raise Exception("❗No hay canciones en la playlist") playing = self.top.value nextSong = self.top.next if nextSong: print(f"⏭️ La siguiente cancion a reproducir es (🎵 {nextSong.value})") else: print(f"Esta es la ultima cancion, no tenemos más en la playlist.") self.top = self.top.next self.length -= 1 if not self.top: self.bottom = None print(f"▶️ Reproduciendo: (🎵 {playing})") return playing def getPlaylist(self): playlist = [] current_song = self.top while current_song: playlist.append(current_song.value) current_song = current_song.next return playlist
SPOILER . . . . . . . . . . . . . . . . . . . .
Mi código:
# node.py class Node: def __init__(self, value): self.value = value self.next = None
# main.py from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_node = Node(song) if not self.top: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length += 1 def playSong(self): if self.length == 0: raise Exception("No hay canciones en la lista") song = self.top.value if self.top == self.bottom: self.bottom = None self.top = self.top.next self.length -= 1 return song def getPlaylist(self): playlist = [] current = self.top while current: playlist.append(current.value) current = current.next return playlist
class Node: def __init__(self, value): self.value = value self.next = None
from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): newSong = Node(song) if self.top == None and self.bottom == None: self.top = newSong self.bottom = newSong else: newSong.next = self.top self.top = newSong self.length += 1 def playSong(self): if self.playlistVoid(): raise Exception("No hay canciones en la lista") if self.top == self.bottom: self.bottom = None playing_song = self.top self.top = self.top.next self.length -= 1 return playing_song.value def playlistVoid(self): return self.length == 0 def getPlaylist(self): songs = [self.playSong() for i in range(self.length)] return songs
class Node: def __init__(self, value): self.value = value self.next =None
from node import Node class Playlist: def __init__(self): self.top =None self.bottom =None self.length = 0 def addSong(self, song): new_song = Node(song) if self.length == 0: self.top = new_song self.bottom = new_song else: new_song.next = self.top self.top = new_song self.length += 1 def playSong(self): if not self.top: raise Exception("No hay canciones en la lista") if self.top == self.bottom: self.bottom =None popped_node = self.top self.top = self.top.next self.length -= 1 return popped_node.value def getPlaylist(self): playlist = [] if not self.top: return playlist current_song = self.top playlist.append(current_song.value) while current_song.next: current_song=current_song.next playlist.append(current_song.value) return playlist playlist = Playlist() playlist.addSong("Bohemian Rhapsody") playlist.addSong("Stairway to Heaven") playlist.addSong("Hotel California") print(playlist.getPlaylist())
class Node: def __init__(self, value): self.value = value self.next = None
from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_node = Node(song) if self.length == 0: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length += 1 def playSong(self): if not self.top: raise Exception("No hay canciones en la lista") if self.top == self.bottom: self.bottom = None popped_node = self.top self.top = self.top.next self.length -= 1 return popped_node.value def getPlaylist(self): play_list = [] current_node = self.top while current_node: play_list.append(current_node.value) current_node = current_node.next return play_list
Solución 😄… .
class Node: def __init__(self, value): self.value = value self.next = None
Main:
from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_song = Node(song) if self.length == 0: self.top = new_song self.bottom = new_song else: new_song.next = self.top self.top = new_song self.length += 1 def playSong(self): if not self.top: raise Exception("No hay canciones en la lista") if self.top == self.bottom: self.bottom = None song = self.top.value self.top = self.top.next self.length -= 1 return song def getPlaylist(self): songList = [] actual = self.top while actual: songList.append(actual.value) actual = actual.next return songList
Mi solución :computer: :snake:
node.py
class Node: def __init__(self, value): self.value = value self.next = None
main.py
from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_song = Node(song) if self.length == 0: self.top = new_song self.bottom = new_song else: new_song.next = self.top self.top = new_song self.length += 1 def playSong(self): if self.length == 0: raise Exception("No hay canciones en la lista") played_song = self.top.value if self.top == self.bottom: self.top = None self.bottom = None else: self.top = self.top.next self.length -= 1 return played_song def getPlaylist(self): if self.length == 0: return [] play_list = [] current = self.top while current: play_list.append(current.value) current = current.next return play_list
playlist = Playlist() playlist.addSong("Bohemian Rhapsody") playlist.addSong("Stairway to Heaven") playlist.addSong("Hotel California") print(playlist.playSong()) # Output: "Hotel California" print(playlist.playSong()) # Output: "Stairway to Heaven" print(playlist.playSong()) # Output: "Bohemian Rhapsody" print(playlist.playSong()) # Output: Error: No hay canciones en
test_add_song_to_empty_playlist test_add_song_to_non_empty_playlist test_throw_error_if_playlist_is_empty test_plays_and_removes_top_song_from_playlist test_returns_empty_array_for_empty_playlist test_returns_array_of_songs_in_reverse_order_of_addition ¡Felicidades, todas las pruebas pasaron!
Node file
class Node: def __init__(self, value): self.value = value self.next = None
Main file
from node import Node class Playlist: def __init__(self): self.top = None self.bottom = None self.length = 0 def addSong(self, song): new_node = Node(song) if self.length == 0: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length += 1 def playSong(self): if not self.top: raise Exception( "No hay canciones en la lista" ) if self.top == self.bottom: self.bottom = None popped_node = self.top self.top = self.top.next self.length -= 1 return popped_node.value def getPlaylist(self): current_node = self.top songs_list = [] if current_node: while current_node: songs_list.append(current_node.value) current_node = current_node.next return songs_list
Mi solución: + + + + + + + +
class Node: def __init__(self, value): # Tu código aquí 👇 self.value = value self.next = None
from node import Node class Playlist: def __init__(self): # Tu código aquí 👇 self.top = None self.bottom = None self.length = 0 def addSong(self, song): # Tu código aquí 👇 new_node = Node(song) if self.length == 0: self.top = new_node self.bottom = new_node else: new_node.next = self.top self.top = new_node self.length +=1 def playSong(self): # Tu código aquí 👇 if not self.top: raise Exception('No hay canciones en la lista') if self.top == self.bottom: self.bottom = None popped_node = self.top self.top =self.top.next self.length -=1 return popped_node.value def getPlaylist(self): # Tu código aquí 👇 if self.length == 0: return [] nodes = [] current_node = self.top while current_node: nodes.append(current_node.value) current_node = current_node.next return nodes
o o o o o
class Node: def __init__(self, value): self.value = value self.next = None class Playlist: def __init__(self): self.top = None self.botton = None self.length = 0 def addSong(self, song): newNode = Node(song) if self.botton == None: self.botton = newNode self.top = newNode else: newNode.next = self.top self.top = newNode self.length += 1 def playSong(self): if self.top == None: raise Exception("No hay canciones en la lista") else: apuntador = self.top cancion = self.top.value self.length -= 1 if self.length == 0: self.bottom = None self.top = None else: self.top = self.top.next del apuntador return cancion def getPlaylist(self): lista = [] apuntador = self.top while apuntador != None: lista.append(apuntador.value) apuntador = apuntador.next return lista playlist = Playlist() print(playlist.getPlaylist()) playlist.addSong("Bohemian Rhapsody") print(playlist.getPlaylist()) playlist.addSong("Stairway to Heaven") print(playlist.getPlaylist()) playlist.addSong("Hotel California") print(playlist.getPlaylist()) print(playlist.playSong()) print(playlist.getPlaylist()) print(playlist.playSong()) print(playlist.getPlaylist()) print(playlist.playSong()) print(playlist.getPlaylist()) print(playlist.playSong())