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
| class MinhaLista: | |
| def __init__(self): | |
| self.__items = [] | |
| self.__index = 0 | |
| def add(self, valor): | |
| self.__items.append(valor) | |
| def __getitem__(self, index): | |
| return self.__items[index] |
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
| import itertools | |
| from pprint import pprint | |
| hotdogs = ['SimpleHotdog', 'SpecialHotdog'] | |
| ingredients = ['Sausage', 'Bacon', | |
| 'Egg', 'Cheese', 'MashedPotatoes', | |
| 'PotatoSticks'] | |
| classnames = [] | |
| for ingredient in ingredients: |
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
| import re | |
| from typing import List, Dict | |
| pessoas: List[Dict[str, str]] = [ | |
| {'nome': 'Luiz', 'sobrenome': 'Miranda'}, | |
| {'nome': 'Maria', 'sobrenome': 'Moreira'}, | |
| {'nome': 'Elaine', 'sobrenome': 'Figueiredo'}, | |
| {'nome': 'Helena', 'sobrenome': 'Oliveira'}, | |
| {'nome': 'vivian', 'sobrenome': 'Silva'}, | |
| {'nome': 'Fabrício', 'sobrenome': 'Costa'}, |
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
| from typing import List | |
| class Caixa: | |
| def __init__(self, tem_chave=False) -> None: | |
| self.tem_chave = tem_chave | |
| def __repr__(self) -> str: | |
| return f'Caixa({self.tem_chave})' |
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
| def fatorial(n: int) -> int: | |
| if n == 1 or n == 0: | |
| return 1 | |
| return n * fatorial(n - 1) | |
| if __name__ == "__main__": | |
| fat5 = fatorial(5) | |
| print(fat5) |
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
| def recursao_infinita(numero: int = 100) -> int: | |
| return recursao_infinita(numero - 1) | |
| if __name__ == "__main__": | |
| recursao_infinita() |
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
| def contagem_regressiva_recursiva(comeca_em: int = 10, termina_em: int = 0) -> int: | |
| """ | |
| Contagem regressiva iniciando em 'comeca_em' e terminando em 'termina_em' | |
| """ | |
| print(comeca_em) | |
| # Caso-base | |
| if comeca_em <= termina_em: | |
| # Perceba que aqui um valor real é retornado | |
| # e não há mais recursão |
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
| from typing import Callable | |
| def nova_funcao(nome: str) -> str: | |
| # Aqui temos um "caso base" | |
| # A função retorna um valor | |
| return f'Oi, {nome}!' | |
| def funcao_anterior(nome: str, funcao: Callable) -> str: |
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
| from functools import lru_cache | |
| @lru_cache | |
| def fibonacci_sequence(n: int) -> int: | |
| """Sequência Fibonacci with memoization""" | |
| if n < 1: | |
| return 0 | |
| if n <= 2: | |
| return 1 |
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
| # Torre de Hanói | |
| def hanoi(disco: int, origem: str, auxiliar: str, destino: str) -> None: | |
| if disco <= 0: | |
| return | |
| hanoi(disco - 1, origem, destino, auxiliar) | |
| print(f'Movendo disco {disco} de {origem} para {destino}') | |
| hanoi(disco - 1, auxiliar, origem, destino) | |
| if __name__ == "__main__": |