{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "matriz_5x5.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyOBnZbcrJkOGKPUHNzd43Dn",
      "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": [
        "<a href=\"https://colab.research.google.com/github/financieras/test/blob/main/matriz_5x5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Matriz 5x5"
      ],
      "metadata": {
        "id": "z4fLvnFOYMf0"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nxBZSbZNRSuP"
      },
      "outputs": [],
      "source": [
        "from math import factorial\n",
        "n = 5   # matriz de orden nxn\n",
        "lista = list(range(1,n+1))   # lista con los elementos a permutar\n",
        "print(lista)\n",
        "\n",
        "n = len(lista)   # n es la longitud de la lista\n",
        "\n",
        "print(f\"El número de permutaciones posibles de {n} elementos es: {factorial(n)}\")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import itertools\n",
        "def permutations(elements):\n",
        "    if len(elements) <= 1:\n",
        "        yield elements\n",
        "        return\n",
        "    for perm in permutations(elements[1:]):\n",
        "        for i in range(len(elements)):\n",
        "            # nb elements[0:1] works in both string and list contexts\n",
        "            yield perm[:i] + elements[0:1] + perm[i:]\n",
        "\n",
        "p = list(permutations(lista))\n",
        "np = len(p)   # p contiene todas las permutaciones posibles de la lista\n",
        "print(np)"
      ],
      "metadata": {
        "id": "nnDkX9XzSISv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from random import seed, randint, sample\n",
        "seed()\n",
        "\n",
        "def comparalo(l, m):   # compara la lista l con las listas de m\n",
        "    distinto = True\n",
        "    for i in range(len(m)):\n",
        "        for j in range(n):\n",
        "            if l[j]==m[i][j]: distinto = False\n",
        "    return distinto\n",
        "\n",
        "def busca():\n",
        "    m = []\n",
        "    for fila in range(n):   # las cuatro filas de la matriz m\n",
        "        cp = p[:]   # cp es una copia de p\n",
        "        cp = sample(cp, np)  # barajamos cp\n",
        "        if fila == 0:   # primera fila de la matriz m\n",
        "            m.append(cp[0])   # la primera candidata se añade a m seguro\n",
        "        else:\n",
        "            for c in cp:   # lista candidata tomada de cp\n",
        "                if comparalo(c, m):\n",
        "                    m.append(c)\n",
        "                    break\n",
        "    return m        \n",
        "\n",
        "if __name__==\"__main__\":\n",
        "    m = busca()\n",
        "    print(m, \"\\n\")\n",
        "    for i in range(n):\n",
        "        print(*m[i])"
      ],
      "metadata": {
        "id": "Fk_UMlI8Wr7v"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import time\n",
        "start = time.perf_counter()\n",
        "\n",
        "def calcula_allm():\n",
        "    allm = []\n",
        "    contador = 0\n",
        "    num = np**2   # si n=5 entonces num=14400\n",
        "    while contador < num+100:\n",
        "        candidata = busca()\n",
        "        if candidata not in allm:\n",
        "            allm.append(candidata)\n",
        "            contador += 1\n",
        "    return allm\n",
        "\n",
        "allm = calcula_allm()\n",
        "end = time.perf_counter()\n",
        "print(f\"Time taken is {end - start}\")\n",
        "\n",
        "#print(sorted(allm))\n",
        "print(len(allm))"
      ],
      "metadata": {
        "id": "pKc4dCMpXmeo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def calcula_allm():\n",
        "    allm = []\n",
        "    contador = 0\n",
        "    num = np**2   # si n=5 entonces num=14400\n",
        "    while contador < 1_000_001:   #num:\n",
        "        candidata = busca()\n",
        "        if candidata not in allm:\n",
        "            allm.append(candidata)\n",
        "            contador += 1\n",
        "            if contador > 160_530: print(contador)\n",
        "    return allm\n",
        "\n",
        "allm = calcula_allm()\n",
        "\n",
        "#print(sorted(allm))\n",
        "print(len(allm))"
      ],
      "metadata": {
        "id": "7LOyuUsElL4z"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}