Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 161 additions & 83 deletions Desafio_01.ipynb
Original file line number Diff line number Diff line change
@@ -1,85 +1,163 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"![](https://i.imgur.com/YX6UATs.png)"
]
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"anaconda-cloud": {},
"colab": {
"name": "Desafio 1.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "SbLLE9q1eldC"
},
"source": [
"### Desafio 1\n",
"\n",
"Escreva um programa em Python para contabilizar a quantidade de ocorrências de cada palavra."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "WhtbdwFseldD"
},
"outputs": [],
"source": [
"palavras = [\n",
" 'red', 'green', 'black', 'pink', 'black', 'white', 'black', 'eyes',\n",
" 'white', 'black', 'orange', 'pink', 'pink', 'red', 'red', 'white', 'orange',\n",
" 'white', \"black\", 'pink', 'green', 'green', 'pink', 'green', 'pink',\n",
" 'white', 'orange', \"orange\", 'red'\n",
"]\n",
"\n",
"\n",
"# Seu código"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "M58o1U9KfAxa"
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"colab": {
"include_colab_link": true,
"name": "Desafio 1.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/BielSousa/br-python-challenges/blob/master/Desafio_01.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SbLLE9q1eldC"
},
"source": [
"### Desafio 1\n",
"\n",
"Escreva um programa em Python para contabilizar a quantidade de ocorrências de cada palavra."
]
},
{
"cell_type": "code",
"metadata": {
"id": "WhtbdwFseldD"
},
"source": [
"palavras = [\n",
" 'red', 'green', 'black', 'pink', 'black', 'white', 'black', 'eyes',\n",
" 'white', 'black', 'orange', 'pink', 'pink', 'red', 'red', 'white', 'orange',\n",
" 'white', \"black\", 'pink', 'green', 'green', 'pink', 'green', 'pink',\n",
" 'white', 'orange', \"orange\", 'red'\n",
"]\n",
"\n",
"\n",
"# Seu código"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_3CxoCU18lwN"
},
"source": [
"#Solução com importando o Counter"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M58o1U9KfAxa",
"outputId": "89bbd9a1-120b-4936-c5f2-2c931e62ffb3"
},
"source": [
"from collections import Counter\n",
"\n",
"print(Counter(palavras))"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Counter({'pink': 6, 'black': 5, 'white': 5, 'red': 4, 'green': 4, 'orange': 4, 'eyes': 1})\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EZOWuzEc9Clb"
},
"source": [
"#Solução criando o Counter"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1RJs2ga39Fuy",
"outputId": "d795db69-c7df-432e-b495-cc9025e8301e",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"def Counter(lista_de_palavras):\n",
" \n",
" palavras_unicas = []\n",
" dicionario_de_palavras = {}\n",
" \n",
" #Faz a contagem de palavras\n",
" for palavra in lista_de_palavras:\n",
" if palavra not in palavras_unicas:\n",
" palavras_unicas.append(palavra)\n",
" count = 0\n",
" for p in lista_de_palavras:\n",
" if p == palavra:\n",
" count+=1\n",
" dicionario_de_palavras[palavra] = count\n",
" \n",
" #Ordena as palavras pela quantidade\n",
" resultado = sorted(dicionario_de_palavras, key = dicionario_de_palavras.get, reverse=True)\n",
" \n",
" #Cria novo dicinario contendo os valores ordenados\n",
" n_dicionario = {}\n",
" for r in resultado:\n",
" n_dicionario[r] = dicionario_de_palavras[r]\n",
"\n",
" #Exporta dicionario \n",
" return n_dicionario\n",
"\n",
"print(Counter(palavras))"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"{'pink': 6, 'black': 5, 'white': 5, 'red': 4, 'green': 4, 'orange': 4, 'eyes': 1}\n"
],
"name": "stdout"
}
]
}
]
}
Loading