{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyPU4lTQDQQRGhaUZhkA6AB5", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Árbol\n" ], "metadata": { "id": "uramGAIr4NqX" } }, { "cell_type": "code", "source": [ "# FUNCIONES\n", "def sa(a,b):\n", " if len(a) > 1: a[0],a[1] = a[1],a[0]\n", " return a,b\n", "def sb(a,b):\n", " if len(b) > 1: b[0],b[1] = b[1],b[0]\n", " return a,b\n", "def ss(a,b):\n", " sa(a,b)\n", " sb(a,b)\n", " return a,b\n", "def pa(a,b):\n", " if len(b) > 0:\n", " a.insert(0, b[0])\n", " b.pop(0)\n", " return a,b\n", "def pb(a,b):\n", " if len(a) > 0:\n", " b.insert(0, a[0])\n", " a.pop(0)\n", " return a,b\n", "def ra(a,b):\n", " if len(a) > 1: a.append(a.pop(0))\n", " return a,b\n", "def rb(a,b):\n", " if len(b) > 1: b.append(b.pop(0))\n", " return a,b\n", "def rr(a,b):\n", " ra(a,b)\n", " rb(a,b)\n", " return a,b\n", "def rra(a,b):\n", " if len(a) > 1: a.insert(0, a.pop())\n", " return a,b\n", "def rrb(a,b):\n", " if len(b) > 1: b.insert(0, b.pop())\n", " return a,b\n", "def rrr(a,b):\n", " rra(a,b)\n", " rrb(a,b)\n", " return a,b" ], "metadata": { "id": "4d6wz9qo4e22" }, "execution_count": 1, "outputs": [] }, { "cell_type": "code", "source": [ "def separa_valor(v):\n", " posicion = v.find('|')\n", " a = v[:posicion]\n", " b = v[posicion+1:]\n", " a = a.replace(\" \", \"\")\n", " b = b.replace(\" \", \"\")\n", " a = [int(x) for x in a]\n", " b = [int(y) for y in b]\n", " return a, b\n", "\n", "def agrega(k, instruccion, aCopy, bCopy):\n", " pareja = ' '.join([str(x) for x in aCopy]) + \"|\" + ' '.join([str(x) for x in bCopy]) # '3 4 2 1|'\n", " tupla = tuple([str(instruccion)])\n", " if pareja not in d.values():\n", " d[k+tupla] = pareja\n", "\n", "def busca(a,b): # Estas son a y b: [4, 3, 2, 1] []\n", " global d\n", " while valor_ordenado not in d.values():\n", " nivel = max([len(k) for k in d.keys()]) # el nivel del árbol es igual a la máxima longitud de las tuplas que van como clave en el diccionario. Inicialmente el nivel=0\n", " d_nivel = {} # inicializamos un diccionario que copia d pero solo los items que sean del nivel máximo en ese momento\n", " for k,v in d.items():\n", " if len(k) == nivel:\n", " d_nivel[k] = v\n", "\n", " for k,v in d_nivel.items():\n", " a, b = separa_valor(v)\n", " \n", " for i in moves:\n", " if (i == \"sa\"): aCopy, bCopy = sa(a[:], b[:])\n", " elif (i == \"sb\"): aCopy, bCopy = sb(a[:], b[:])\n", " elif (i == \"ss\"): aCopy, bCopy = ss(a[:], b[:])\n", " elif (i == \"pa\"): aCopy, bCopy = pa(a[:], b[:])\n", " elif (i == \"pb\"): aCopy, bCopy = pb(a[:], b[:])\n", " elif (i == \"ra\"): aCopy, bCopy = ra(a[:], b[:])\n", " elif (i == \"rb\"): aCopy, bCopy = rb(a[:], b[:])\n", " elif (i == \"rr\"): aCopy, bCopy = rr(a[:], b[:])\n", " elif (i == \"rra\"): aCopy, bCopy = rra(a[:], b[:])\n", " elif (i == \"rrb\"): aCopy, bCopy = rrb(a[:], b[:])\n", " elif (i == \"rrr\"): aCopy, bCopy = rrr(a[:], b[:])\n", " agrega(k, i, aCopy, bCopy)\n", "\n", "if __name__ == \"__main__\":\n", " a_string = \"6 5 4 3 2 1\"\n", " a = a_string.replace(\" \", \"\") # '4321'\n", " d = {(): a_string + \"|\"} # diccionario. Ejemplo: {():'4 3 2 1|', ('sa',):'3 4 2 1|', ('pb',):'3 2 1|4', ('ra',):'3 2 1 4|', ('rra',):'1 4 3 2|', ('pb', 'sa'):'2 3 1|4', ('pb', 'pb'):'2 1|3 4'}\n", " moves = [\"ra\",\"rb\",\"rra\",\"rrb\",\"pa\",\"pb\",\"sa\",\"sb\",\"ss\",\"rr\",\"rrr\"]\n", " a = [int(x) for x in a] # [4, 3, 2, 1]\n", " b = []\n", " valor_ordenado = ' '.join(map(str, sorted(a))) + '|' # \"1 2 3 4|\"\n", " busca(a,b)\n", " print(list(d.keys())[list(d.values()).index(valor_ordenado)]) # da la clave que corresponde al valor ordenado \"1 2 3 4|\"\n", " #print(d)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7gsSuWyf5LYM", "outputId": "dd4cd75b-c605-4eba-f838-1860f152c42d" }, "execution_count": 21, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('rra', 'rra', 'pb', 'pb', 'sa', 'ra', 'ra', 'ss', 'pa', 'pa')\n" ] } ] }, { "cell_type": "code", "source": [ "!pip install openpyxl" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K63Z_qrE4yW8", "outputId": "07640230-e72e-4d38-f2d5-c8dc56760b5a" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: openpyxl in /usr/local/lib/python3.8/dist-packages (3.0.10)\n", "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.8/dist-packages (from openpyxl) (1.1.0)\n" ] } ] }, { "cell_type": "code", "source": [ "import openpyxl # importamos la librería openpyxl\n", "\n", "wb = openpyxl.Workbook() # para crear por primera vez un libro\n", "ws = wb.active # estas dos lineas crean el libro, aún sin nombre\n", "\n", "wb.save('arbol.xlsx') # grabamos el fichero por primera vez" ], "metadata": { "id": "0JjKGTof45Sh" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "# Cuando ya sebemos que queremos trabajar con el archivo arbol.xlsx\n", "wb = openpyxl.load_workbook('arbol.xlsx')\n", "\n", "ws = wb[\"Sheet\"] # accediendo a la hoja Sheet\n", "\n", "fila = 11\n", "for k,v in d.items():\n", " ws.cell(row=fila, column=3).value = str(k)\n", " ws.cell(row=fila, column=4).value = v\n", " fila += 1\n", "\n", "wb.save('arbol.xlsx')\n", "wb.close()" ], "metadata": { "id": "mvYTCFDxZcxR" }, "execution_count": 5, "outputs": [] } ] }