Created
July 27, 2019 19:16
-
-
Save orejuelajd/ced19e1f53c8fa8a2ea32c031c9aab03 to your computer and use it in GitHub Desktop.
This is a simple console bot to put in a line orders from delivery companies like rappi, ubereats etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definimos la clase básica para un nodo donde se almacena | |
# la data del mismo nodo | |
# el index del siguiente nodo | |
# el index del nodo anterior | |
class Node: | |
def __init__(self, data, name, product): | |
self.data = data | |
self.name = name | |
self.product = product | |
self.next = None | |
self.prev = None | |
# Se crea la clase de la lista doblemente linkeada | |
class DoublyLinkedList: | |
# Constructor | |
def __init__(self): | |
self.first = None | |
self.last = None | |
# Se ingresa el indice del nodo que se quiere obtener | |
def get_node(self, index): | |
# el nodo current es el primero en la lista | |
current = self.first | |
# Se recorre la lista hasta el indice indicado | |
for i in range(index): | |
# Si el nodo actual es none es porque la lista esta vacia | |
if current is None: | |
# se devuelve none ya que no hay nodos | |
return None | |
# Si el nodo actual no es none se obtiene el siguiente | |
# para seguir recorriendo la lista hasta llegar al indice solicitado | |
current = current.next | |
return current | |
# ingresar el nodo despues de un nodo de referencia | |
# se ingresa el indice del nodo de referencia | |
def insert_after(self, ref_node, new_node): | |
new_node.prev = ref_node | |
if ref_node.next is None: | |
self.last = new_node | |
else: | |
new_node.next = ref_node.next | |
new_node.next.prev = new_node | |
ref_node.next = new_node | |
def insert_before(self, ref_node, new_node): | |
new_node.next = ref_node | |
if ref_node.prev is None: | |
self.first = new_node | |
else: | |
new_node.prev = ref_node.prev | |
new_node.prev.next = new_node | |
ref_node.prev = new_node | |
def insert_at_beg(self, new_node): | |
if self.first is None: | |
self.first = new_node | |
self.last = new_node | |
else: | |
self.insert_before(self.first, new_node) | |
def insert_at_end(self, new_node): | |
if self.last is None: | |
self.last = new_node | |
self.first = new_node | |
else: | |
self.insert_after(self.last, new_node) | |
def remove(self, node): | |
if node.prev is None: | |
self.first = node.next | |
else: | |
node.prev.next = node.next | |
if node.next is None: | |
self.last = node.prev | |
else: | |
node.next.prev = node.prev | |
def display(self): | |
current = self.first | |
while current: | |
print(current.data, current.name, current.product, end=',\n') | |
current = current.next | |
a_dllist = DoublyLinkedList() | |
print('Menú de pedidos de Rappi') | |
print('insertar <data> <name> <product> despues <index>') | |
print('insertar <data> <name> <product> antes <index>') | |
print('insertar <data> <name> <product> en principio') | |
print('insertar <data> <name> <product> en final') | |
print('quitar <index>') | |
print('salir') | |
while True: | |
print('La lista: ', end='') | |
a_dllist.display() | |
print() | |
do = input('Que te gustaria hacer? ').split() | |
operation = do[0].strip().lower() | |
if operation == 'insertar': | |
data = int(do[1]) | |
name = do[2] | |
product = do[3] | |
position = do[5].strip().lower() | |
new_node = Node(data, name, product) | |
suboperation = do[4].strip().lower() | |
if suboperation == 'en': | |
if position == 'principio': | |
a_dllist.insert_at_beg(new_node) | |
elif position == 'final': | |
a_dllist.insert_at_end(new_node) | |
else: | |
index = int(position) | |
ref_node = a_dllist.get_node(index) | |
if ref_node is None: | |
print('No such index.') | |
continue | |
if suboperation == 'despues': | |
a_dllist.insert_after(ref_node, new_node) | |
elif suboperation == 'antes': | |
a_dllist.insert_before(ref_node, new_node) | |
elif operation == 'quitar': | |
index = int(do[1]) | |
node = a_dllist.get_node(index) | |
if node is None: | |
print('No hay indice.') | |
continue | |
a_dllist.remove(node) | |
elif operation == 'quit': | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment