def main3():
    k = Stack()
    k.push(47)
    k.push(42)
    k.push(49)
    k.push(44)
    k.push(33)
    k.push(21)
    k.push(90)
    k.push(69)
    k.push(59)
    k.push(74)
    k.push(26)
    k.push(35)
    print("*******************")
    print(k.to_string())
    print("*******************")
    eliminar_medio(
        k
    )  #saca el valor de la mitad de la pila pero enpesando a contar del tope
from pilas import Stack

pila = Stack()
pila.push("a")
pila.push("x")
pila.to_string()
pila.push("b")
pila.push("y")
pila.to_string()
var = pila.pop()
pila.to_string()
print(f"var={var}")
Beispiel #3
0
class LaberintoADT:
    def __init__(self, archivo_inicio):
        archivo = open(archivo_inicio, 'rt')
        self.__laberinto = Array2D(int(archivo.readline().strip()),
                                   int(archivo.readline().strip()), '1')
        self.__laberinto.clearing('1')  # todo pared
        self.__entrada = []
        self.__salida = []
        self.__camino = Stack()
        self.__previa = None

        lineas = archivo.readlines()
        for ren in range(len(lineas)):
            lineas[ren] = lineas[ren].strip()
            col = 0
            for cell in lineas[ren].split(','):
                self.__laberinto.set_item(ren, col, cell)
                col += 1

    def set_previa(self, posicion):
        self.__previa = posicion

    def get_previa(self):
        return self.__previa

    def mostrar_laberinto(self):
        #self.__laberinto.to_string()
        for ren in range(self.__laberinto.get_num_rows()):
            for col in range(self.__laberinto.get_num_cols()):
                #print(self.string_to_wall(self.__laberinto.get_item(ren,col)),end='')
                self.string_to_wall(self.__laberinto.get_item(ren, col))
            print("")

    def mostrar_actual(self):
        lab = self.__laberinto
        actual = self.__camino.peek()
        lab.set_item(actual[0], actual[1], 'A')

        for ren in range(lab.get_num_rows()):
            for col in range(lab.get_num_cols()):
                self.string_to_wall(lab.get_item(ren, col))

            print("")

    def string_to_wall(self, celda):
        if celda == '1':
            print("{} {}{}".format(bg('white'), ' ', attr("reset")), end='')
        elif celda == '0':
            print("{} {}{}".format(bg('black'), ' ', attr("reset")), end='')
        elif celda == 'E':
            print("{} {}{}".format(bg('blue'), 'E', attr("reset")), end='')
        elif celda == 'S':
            print("{} {}{}".format(bg('green'), 'S', attr("reset")), end='')
        elif celda == 'C':
            print("{} {}{}".format(bg('light_green'), ' ', attr("reset")),
                  end='')
        elif celda == 'A':
            print("{} {}{}".format(bg('light_green'), ' ', attr("reset")),
                  end='')
        else:
            print("{} {}{}".format(bg('red'), ' ', attr("reset")), end='')

    def set_entrada(self):
        encontrada = False
        for ren in range(self.__laberinto.get_num_rows()):
            for col in range(self.__laberinto.get_num_cols()):
                if self.__laberinto.get_item(ren, col) == 'E':
                    self.__entrada = [ren, col]
                    encontrada = True
                    self.__camino.push([ren, col])
        return encontrada

    def set_salida(self):
        encontrada = False
        for ren in range(self.__laberinto.get_num_rows()):
            for col in range(self.__laberinto.get_num_cols()):
                if self.__laberinto.get_item(ren, col) == 'S':
                    self.__salida = [ren, col]
                    encontrada = True
        return encontrada

    def agragar_solucion(self):
        while not self.__camino.is_empty():
            paso = self.__camino.pop()
            self.__laberinto.set_item(paso[0], paso[1], 'C')

    def resolver(self):
        actual = self.__camino.peek()

        # revisar izq
        if self.__laberinto.get_item(actual[0],actual[1]-1) == '0' \
            and self.__laberinto.get_item(actual[0],actual[1]-1) != 'X' \
            and self.get_previa() != [actual[0],actual[1]-1]:
            #print('mover izq')
            self.set_previa(actual)
            self.__camino.push([actual[0], actual[1] - 1])
        # revisar arriba
        elif self.__laberinto.get_item(actual[0]-1,actual[1]) == '0'  \
        and self.__laberinto.get_item(actual[0]-1,actual[1]) != 'X' \
        and self.get_previa() != [actual[0]-1,actual[1]]:
            #print('mover arriba')
            self.set_previa(actual)
            self.__camino.push([actual[0] - 1, actual[1]])
        # revisar der
        elif self.__laberinto.get_item(actual[0],actual[1]+1) == '0' \
        and self.__laberinto.get_item(actual[0],actual[1]+1) != 'X' \
        and self.get_previa() != [actual[0],actual[1]+1] :
            #print('mover derecha')
            self.set_previa(actual)
            self.__camino.push([actual[0], actual[1] + 1])
        # revisar abajo
        elif self.__laberinto.get_item(actual[0]+1,actual[1]) == '0' \
        and self.__laberinto.get_item(actual[0]+1,actual[1]) != 'X' \
        and self.get_previa() != [actual[0]+1,actual[1]]:
            #print('mover abajo')
            self.set_previa(actual)
            self.__camino.push([actual[0] + 1, actual[1]])
        else:
            self.__laberinto.set_item(actual[0], actual[1], 'X')
            self.__camino.pop()

    def imprime_camino(self):
        self.__camino.to_string()

    def es_la_salida(self):
        actual = self.__camino.peek()
        try:
            if self.__laberinto.get_item(actual[0], actual[1] - 1) == 'S':
                return True
            elif self.__laberinto.get_item(actual[0] - 1, actual[1]) == 'S':
                return True
            elif self.__laberinto.get_item(actual[0], actual[1] + 1) == 'S':
                return True
            elif self.__laberinto.get_item(actual[0] + 1, actual[1]) == 'S':
                return True
            else:
                return False

        except Exception as e:
            print(f"Error:{e}")
from pilas import Stack

pila = Stack()
pila.push('a')
pila.push('x')
pila.to_string()
pila.push('b')
pila.push('y')
pila.to_string()
var = pila.pop()
pila.to_string()
print(f"var = {var} ")
Beispiel #5
0
from cola import Cola
queue = Cola()

queue.put('Hola')
queue.put('que')
queue.put('hace')
print(queue.isEmpty())
print(queue.items)
print(queue.size())
queue.get()
print(queue.items)
print(queue.size())
queue.get()
queue.get()
print(queue.isEmpty())

from pilas import Stack

pila = Stack()
pila.push('Hello')
pila.push('World')
print(pila.stack)
print(pila.remove())
pila.peek()
print(pila.stack)
print(pila.remove())
print(pila.stack)
print(pila.remove())
Beispiel #6
0
from pilas import Stack

pl = Stack()
pl.push('a')
pl.push('x')
pl.to_string()
pl.push('b')
pl.push('y')
pl.to_string()
var = pl.pop()
pl.to_string()
print(f"var={var}")
Beispiel #7
0
        cuenta_regresiva(num)


def elim_pos_media(pila, size, curr):
    if (pila.is_empty() or curr == size):
        return
    x = pila.peek()
    pila.pop()
    elim_pos_media(pila, size, curr + 1)
    m = (size / 2)
    if m == int(m):
        if (curr != m and curr != (m - 1)):
            pila.push(x)
    else:
        if (curr != int(size / 2)):
            pila.push(x)


if __name__ == '__main__':
    cuenta_regresiva(5)
    pila = Stack()
    pila.push(9)
    pila.push(6)
    pila.push(3)
    pila.push(2)

    print(pila.to_string())
    l = pila.length()

    elim_pos_media(pila, l, 0)