Created
February 23, 2023 21:27
-
-
Save danielomiya/6f3a77a7174026872850f8c2533bc1b0 to your computer and use it in GitHub Desktop.
A2_Python_Exerc.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "A2_Python_Exerc.ipynb", | |
| "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": [ | |
| "<a href=\"https://colab.research.google.com/gist/gwyddie/6f3a77a7174026872850f8c2533bc1b0/a2_python_exerc.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "pqEhMdFQO3A-" | |
| }, | |
| "source": [ | |
| "<head>\n", | |
| " <meta name=\"author\" content=\"Rogério de Oliveira\">\n", | |
| " <meta institution=\"author\" content=\"Universidade Presbiteriana Mackenzie\">\n", | |
| "</head>\n", | |
| "\n", | |
| "<img src=\"http://meusite.mackenzie.br/rogerio/mackenzie_logo/UPM.2_horizontal_vermelho.jpg\" width=300, align=\"right\"> \n", | |
| "\n", | |
| "\n", | |
| "\n", | |
| "<br>\n", | |
| "<br>\n", | |
| "\n", | |
| "<br>\n", | |
| "<br>\n", | |
| "\n", | |
| "\n", | |
| "<br>\n", | |
| "<br>\n", | |
| "\n", | |
| "---\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "42yF1G-yHu98" | |
| }, | |
| "source": [ | |
| "# Exercícios" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "BDm3FTRtWXCY" | |
| }, | |
| "source": [ | |
| "Em todos os exercícios execute o código abaixo como a primeira instrução e uma única vez para gerar os valores de `letras` e `lista`.\n", | |
| "\n", | |
| "Todos exercícios possuem várias soluções possíveis, mas não se preocupe com isso. A melhor solução é primeiro, a que funciona para você!" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "xBjopVrbVgb5", | |
| "outputId": "0c35b3b7-376c-4075-fa9c-7fd073d2a87c" | |
| }, | |
| "source": [ | |
| "import numpy as np\n", | |
| "import random\n", | |
| "from random import choices\n", | |
| "import string\n", | |
| "\n", | |
| "random.seed(1234)\n", | |
| "letras = choices(string.ascii_lowercase,k=100)\n", | |
| "lista = random.sample(range(1, 10000), 1000)\n", | |
| "print(np.array(lista).sum() == 5244799) # precisa retornar True!" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "True\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "XEngnerfXmKX" | |
| }, | |
| "source": [ | |
| "1. Dos valores [9887, 3233, 813] qual o índice dos valores presentes em `lista`? " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "8KlbNKnHR6VZ", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 218 | |
| }, | |
| "outputId": "c341ef69-02a6-435e-d05e-c0eac5c159b9" | |
| }, | |
| "source": [ | |
| "for idx in (9887, 3233, 813):\n", | |
| " print(lista.index(idx))" | |
| ], | |
| "execution_count": 2, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "877\n", | |
| "680\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "error", | |
| "ename": "ValueError", | |
| "evalue": "ignored", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m<ipython-input-2-526df33b4abd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0midx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m9887\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3233\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m813\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlista\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0midx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[0;31mValueError\u001b[0m: 813 is not in list" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "95H7DlB_YARA" | |
| }, | |
| "source": [ | |
| "2. Quais os dois primeiros valores maiores que 2046 em `lista`?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "BknXd3WYST7B", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "63af9e7b-75e8-4dce-bdc5-ffd247d0831b" | |
| }, | |
| "source": [ | |
| "tmp = iter(sorted(elem for elem in lista if elem > 2046))\n", | |
| "next(tmp), next(tmp)" | |
| ], | |
| "execution_count": 3, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(2055, 2064)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 3 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "_i-bSWW9YNa_" | |
| }, | |
| "source": [ | |
| "3. Quais os valores mínimo e máximo em `lista`? " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "wnhzxGXfQr6Q", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "2885278e-73c5-48ad-9f56-692c870ce7e8" | |
| }, | |
| "source": [ | |
| "min(lista), max(lista)" | |
| ], | |
| "execution_count": 4, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(4, 9998)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 4 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "Fs5Ys1kybzDz" | |
| }, | |
| "source": [ | |
| "4. Quais as 4 letras que mais aparecem em `letras`?\n", | |
| "\n", | |
| "<small> ( Existe várias formas de fazer isso, mas fica aqui a sugestão: se você implementar um dicionário *letra:quantidade* poderá empregar a mesma solução na tarefa de aprofundamento! ) </small> " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "dna43p-3TcPH", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "2c10b2de-701b-4434-870f-48cab94a7379" | |
| }, | |
| "source": [ | |
| "from collections import defaultdict\n", | |
| "from operator import itemgetter\n", | |
| "\n", | |
| "\n", | |
| "freq = defaultdict(lambda: 0)\n", | |
| "for letra in letras:\n", | |
| " freq[letra] += 1\n", | |
| "\n", | |
| "\n", | |
| "tmp = iter(sorted(freq.items(), key=itemgetter(1), reverse=True))\n", | |
| "next(tmp), next(tmp), next(tmp), next(tmp)" | |
| ], | |
| "execution_count": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(('e', 8), ('v', 8), ('m', 7), ('a', 6))" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 5 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "cdADnpZFZKxg" | |
| }, | |
| "source": [ | |
| "5. Qual a soma dos valores pares de `lista`?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "2MOHct7HZIWo", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "3099ddeb-dcf0-4ba5-b71c-ed419b4391df" | |
| }, | |
| "source": [ | |
| "acc = 0\n", | |
| "\n", | |
| "for elem in lista:\n", | |
| " if elem % 2 == 0:\n", | |
| " acc += elem\n", | |
| "\n", | |
| "acc" | |
| ], | |
| "execution_count": 6, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "2687196" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 6 | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment