{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "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": "code", "source": [ "# PASOS NECESARIOS PARA COLOCAR CADA ELEMENTO DE A EN SU SITIO EN B total = pasosA + pasosB (esta suma es solo una idea, se ha de sumar de una forma peculiar)\n", "\n", "def necesariosA(a, b): # array pasosA calcula los pasos necesarios para colocar cada elemento de A como el primero de su pila\n", " for v in a:\n", " if a.index(v) < len(a)/2:\n", " pasosA.append(a.index(v))\n", " else:\n", " pasosA.append(-(len(a) - a.index(v)))\n", "\n", "def necesariosB(a,b):\n", " for i in range(len(a)):\n", " maxinfB = min(b) # maximo de los inferiores en B al valor iésimo de A, se inicializa en -infinito(-2147483648) o si da problemas se inicializa en el min(b)\n", " for j in range(len(b)):\n", " if b[j] < a[i] and b[j] > maxinfB:\n", " maxinfB = b[j]\n", " # el maxinfB se ha de situar el primero de la pila B \n", " if b.index(maxinfB) < len(b)/2:\n", " pasosB.append(b.index(maxinfB))\n", " else:\n", " pasosB.append(-(len(b)- b.index(maxinfB)))\n", "\n", "def totaliza(a,b): # totalizar pasos\n", " global total\n", " for i in range(len(pasosA)):\n", " if pasosA[i] * pasosB[i] < 0: # si son de distinto signo, uno positivo y otro negativo\n", " total.append(abs(pasosA[i]) + abs(pasosB[i])) # no hay sinergia\n", " else: # si son de igual signo o alguno cero\n", " total.append(max(abs(pasosA[i]), abs(pasosB[i]))) # si son de igual signo hay sinergia\n", "\n", "def calculaIndexPasosMinimos():\n", " global pasosA\n", " global pasosB\n", " global total\n", " pasosA = [] # [0,1,2, ...,41,-41,... , -2,-1] vector donde cada index está asociado con el valor del mismo index en A\n", " pasosB = [] # [-3,2,-5, ..., -5,0] estos dos arrays se han de recalcular cada vez que realmente se mueva algún elemento de A a B\n", " total = []\n", " necesariosA(a, b); print(\"pasosA: \", pasosA)\n", " necesariosB(a, b); print(\"pasosB: \", pasosB)\n", " totaliza(a,b); print(\"total: \", total)\n", " return total.index(min(total)) # retorna el índice del elemento de la pila A que menos pasos totales necesita" ], "metadata": { "id": "EcyWE3LvlUQA" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "a = [3, 23, 24, 10, 15, 32, 30, 29, 6, 26, 25, 20, 27, 12, 17, 19, 21, 18, 28, 31, 9, 2]\n", "b = [13,11,8,7,5,4,1,22,16,14]" ], "metadata": { "id": "yzEohnqVjVsH" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "if __name__ == \"__main__\":\n", " index_minimosPasos = calculaIndexPasosMinimos()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eh_xX5MTk8zv", "outputId": "995635ba-0d83-49e9-c221-e19448a72a44" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "pasosA: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]\n", "pasosB: [-4, -3, -3, 2, -1, -3, -3, -3, 4, -3, -3, -2, -3, 1, -2, -2, -2, -2, -3, -3, 2, -4]\n", "total: [4, 4, 5, 3, 5, 8, 9, 10, 8, 12, 13, 11, 10, 10, 8, 7, 6, 5, 4, 3, 4, 4]\n" ] } ] } ] }