{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNmxQOXM9N8jP2wRFzssVOv", "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": [ "# Función que detecta si una lista está ordenada\n", "## de forma decreciente circular" ], "metadata": { "id": "8xP09_8ocjxm" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qPJppg9Gch9Y" }, "outputs": [], "source": [ "def ordenado_circular_decreciente(arr): # da True si todos los elementos de la pila están ordenados de forma decreciente circular\n", " maximo, minimo = max(arr), min(arr)\n", " flags = [False]*(len(arr)-1)\n", " for i in range(len(arr)-1):\n", " if arr[i]==minimo and arr[i+1]==maximo:\n", " flags[i]= True\n", " elif arr[i] > arr[i+1]:\n", " flags[i] = True\n", " return all(flags)" ] }, { "cell_type": "markdown", "source": [ "## de forma creciente circular" ], "metadata": { "id": "tmC5PG9hcVWe" } }, { "cell_type": "code", "source": [ "def ordenado_circular_creciente(arr): # da True si todos los elementos de la pila están ordenados de forma creciente circular\n", " maximo, minimo = max(arr), min(arr)\n", " flags = [False]*(len(arr)-1)\n", " for i in range(len(arr)-1):\n", " if arr[i]==maximo and arr[i+1]==minimo:\n", " flags[i]= True\n", " elif arr[i] < arr[i+1]:\n", " flags[i] = True\n", " return all(flags) and arr[0] > arr[-1]" ], "metadata": { "id": "dhF8CnM2cUtQ" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "#a = [6,7,8,9,1,2,3,5] # True\n", "a = [21, 22, 24, 26, 28, 29, 33, 3, 7, 13, 18, 19]\n", "ordenado_circular_creciente(a)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Z0QTdL0fcu3Z", "outputId": "f0326c50-8c08-491d-e7dd-93f509d53d5d" }, "execution_count": 6, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "markdown", "source": [ "## Tramo creciente (no circular)\n", "Esta función proporciona el tramo más largo de números crecientes. \n", "En caso de empate se muestra el último tramo. \n", "Si se quiere el primer tramo cambiar en dos sitios el <= por un <. " ], "metadata": { "id": "K07AzOgpf04v" } }, { "cell_type": "code", "source": [ "def crecientes(arr):\n", " l = [] # lista creciente actual de trabajo \n", " result = [] # lista creciente más larga, al final es el resultado que se retorna\n", " previo = None\n", " for actual in arr:\n", " if previo is None or actual > previo: # al seguir ascendiendo\n", " l.append(actual) # agregar a la lista actual\n", " else: # si ya no asciende\n", " if len(l) >= len(result): # si la lista actual l es más larga que la que hasta ese momento existía en result\n", " result = l[:] # la lista antigua result desaparece y es sustituida por la la lista actual l\n", " l = [actual]\n", " previo = actual\n", " if l and len(l)>=len(result): # ver el última tramo, si hay algo allí. El = hace que en caso de empate se tome el tramo más a la derecha\n", " result = l[:]\n", " return result\n", " \n", "a = [46,31,30,32,37,23,38,27,8,25,48,15,44,20,45,9,14,16,39,41,43,47,2,5,10,1,17,4,11,40,12,21,22,24,26,28,29,33,3,7,13,18,19,6,34,35,42,36]\n", "print(crecientes(a))" ], "metadata": { "id": "iAFxuu5xjf1R", "outputId": "d2dd5f02-6829-4291-c18e-bb4325609467", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[12, 21, 22, 24, 26, 28, 29, 33]\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Tramo creciente circular\n", "Esta función proporciona el tramo más largo de números crecientes circulares.\n", "* Ejemplo: [9, 14, 16, 39, 41, 43, 47, 2, 5]\n", "* Ejemplo: [21, 22, 24, 26, 28, 29, 33, 3, 7, 13, 18, 19]" ], "metadata": { "id": "7Fmy_gSg4lE6" } }, { "cell_type": "code", "source": [ "def ordenado_circular_creciente(arr): # da True si todos los elementos de la pila están ordenados de forma creciente circular\n", " maximo, minimo = max(arr), min(arr)\n", " flags = [False]*(len(arr)-1)\n", " for i in range(len(arr)-1):\n", " if arr[i]==maximo and arr[i+1]==minimo:\n", " flags[i]= True\n", " elif arr[i] < arr[i+1]:\n", " flags[i] = True\n", " return all(flags) and arr[0] > arr[-1]\n", "\n", "def tramo_creciente_circular(arr):\n", " n = len(arr)\n", " optimo = [] # tramo más amplio\n", " for i in range(n-1):\n", " for j in range(i+1,n):\n", " if ordenado_circular_creciente(arr[i:j]) and len(arr[i:j]) > len(optimo):\n", " optimo = arr[i:j]\n", " return optimo\n", "\n", "a = [46,31,30,32,37,23,38,27,8,25,48,15,44,20,45,9,14,16,39,41,43,47,2,5,10,1,17,4,11,40,12,21,22,24,26,28,29,33,3,7,13,18,19,6,34,35,42,36]\n", "print(tramo_creciente_circular(a))" ], "metadata": { "id": "yJTfIS5e7iDL", "outputId": "fea63f00-7538-4720-f975-8d014aecf914", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[21, 22, 24, 26, 28, 29, 33, 3, 7, 13, 18, 19]\n" ] } ] } ] }