Last active
January 28, 2020 18:10
-
-
Save henriquebastos/9e467800cccbe8d175de7dffa274f303 to your computer and use it in GitHub Desktop.
Assine meu canal no youtube e me envie sugestões de novos exercícios: http://hbn.link/youtube_HB
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"# Fatorial em Python com 1 linha\n", | |
"\n", | |
"## Introdução\n", | |
"\n", | |
"O Victor Siqueira postou um exercício no [Fórum do Welcome to the Django](https://welcometothedjango.com.br) para calcular o fatorial de um número.\n", | |
"\n", | |
"A discussão foi maneira e tem umas boas sacadas para você aprimorar suas habilidades Pythônicas.\n", | |
"\n", | |
"\n", | |
"## Autor\n", | |
"\n", | |
"[Henrique Bastos](https://henriquebastos.net)\n", | |
"\n", | |
"\n", | |
"Se conecte comigo nas redes:\n", | |
"[YouTube](https://hbn.link/youtube_HB), [Facebook](https://hbn.link/fanpage_HB), [Twitter](https://hbn.link/twitter_HB), [Instagram](https://hbn.link/instagram_HB), [Whatsapp](https://hbn.link/hb-ajudae), [Telegram](https://t.me/henriquebastos), [Email](mailto:[email protected])\n", | |
"\n", | |
"Assista ao video demonstrando o exercício:\n", | |
"\n", | |
"[](https://youtu.be/rPj9iQrOpPw)\n", | |
"\n", | |
"\n", | |
"## Licença\n", | |
"\n", | |
"<img alt=\"Licença Creative Commons\" style=\"border-width:0; margin-left:0;\" src=\"https://i.creativecommons.org/l/by-sa/4.0/88x31.png\" />\n", | |
"\n", | |
"Este trabalho está licenciado com uma Licença [Creative Commons - Atribuição-CompartilhaIgual 4.0 Internacional](http://creativecommons.org/licenses/by-sa/4.0/)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"# FirstFactorial\n", | |
"\n", | |
"A função `FirstFactorial(num)` recebe `num` como parametro e retorna o fatorial desse número.\n", | |
"\n", | |
"Por exemplo: \n", | |
"\n", | |
"Se `num = 4`, então seu programa deve retornar `(4 * 3 * 2 * 1) = 24`.\n", | |
"\n", | |
"Para os casos de teste, o intervalor será entre 1 e 18 e a entrada será sempre um inteiro." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"24" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Essa expressão abaixo calcula o fatorial do número 4 em Python.\n", | |
"4 * 3 * 2 * 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"## Soluções\n", | |
"\n", | |
"Aqui está o código do Victor Siqueira que inspirou esse exercício. _Valeu d+, Victor!_" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4\n", | |
"24\n" | |
] | |
} | |
], | |
"source": [ | |
"def FirstFactorial(num):\n", | |
" if num == 1:\n", | |
" return num\n", | |
" \n", | |
" result = 1\n", | |
" \n", | |
" for i in reversed(range(num)):\n", | |
" result = result * (i + 1)\n", | |
" \n", | |
" return result\n", | |
"\n", | |
"\n", | |
"print(FirstFactorial(int(input())))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Abaixo temos uma segunda versão com melhorias usando a estratégia do Victor." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"def FirstFactorial(num):\n", | |
" factorial = 1\n", | |
" \n", | |
" for n in range(1, num + 1):\n", | |
" factorial *= n\n", | |
" \n", | |
" return factorial\n", | |
"\n", | |
"\n", | |
"assert FirstFactorial(1) == 1\n", | |
"assert FirstFactorial(4) == 24" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Por último temos a terceira versão reduzindo a mesma lógica do Victor para apenas uma linha utilizando alguns recursos mais avançados do Python." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"from functools import reduce\n", | |
"from operator import mul\n", | |
"\n", | |
"\n", | |
"def FirstFactorial(num):\n", | |
" return reduce(mul, range(1, num + 1))\n", | |
"\n", | |
"\n", | |
"assert FirstFactorial(1) == 1\n", | |
"assert FirstFactorial(4) == 24" | |
] | |
} | |
], | |
"metadata": { | |
"celltoolbar": "Slideshow", | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment