-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from vbuxbaum/cap-paradigmas
Cap paradigmas - Implementações 1 a 6
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ArvoreBinaria: | ||
def __init__(self): | ||
self.raiz = self.No() | ||
|
||
def central(cls, no_pesquisado): | ||
if no_pesquisado is not None: | ||
cls.central(no_pesquisado.esquerda) | ||
print(no_pesquisado.valor) | ||
cls.central(no_pesquisado.direita) | ||
|
||
class No: | ||
def __init__(self): | ||
self.valor = None | ||
self.esquerda = None | ||
self.direita = None |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Implementado no arquivo 1_arvore_central.py | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Fibonacci: | ||
@classmethod | ||
def recursivo(cls, n): | ||
if n < 2: | ||
return n | ||
else: | ||
return cls.recursivo(n - 1) + cls.recursivo(n - 2) | ||
|
||
@classmethod | ||
def iterativo(cls, n): | ||
primeiro = 0 | ||
segundo = 1 | ||
for _ in range(n): | ||
primeiro = segundo + primeiro | ||
segundo = primeiro - segundo | ||
return primeiro | ||
|
||
|
||
if __name__ == "__main__": | ||
teste_recursivo = Fibonacci.recursivo(7) | ||
teste_iterativo = Fibonacci.iterativo(7) | ||
|
||
assert teste_recursivo == 13 | ||
assert teste_iterativo == 13 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Implementado no arquivo 3_fibonacci.py | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Esta implementação do livro é apenas um pseudo-código. A implementação 6 | ||
(6_passeio_cavalo.py) apresentação a solução real. | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from pprint import pprint | ||
|
||
|
||
class PasseioCavalo: | ||
PASSOS = [ | ||
(2, 1), | ||
(1, 2), | ||
(-1, 2), | ||
(-2, 1), | ||
(-2, -1), | ||
(-1, -2), | ||
(1, -2), | ||
(2, -1), | ||
] | ||
|
||
def __init__(self, tamanho_tabuleiro, inicio=(0, 0)): | ||
self.tamanho_tabuleiro = tamanho_tabuleiro | ||
self.tabuleiro = [ | ||
[0 for _ in range(tamanho_tabuleiro)] | ||
for _ in range(tamanho_tabuleiro) | ||
] | ||
self.tabuleiro[inicio[0]][inicio[1]] = 1 | ||
|
||
def tenta(self, passo_n, pos_x, pos_y): | ||
for direcao in self.PASSOS: | ||
ctrl_sucesso = False | ||
|
||
pos_u = pos_x + direcao[0] | ||
pos_v = pos_y + direcao[1] | ||
|
||
if self.posicao_valida(pos_u, pos_v): | ||
self.preenche(passo_n, pos_u, pos_v) | ||
ctrl_sucesso = self.resolve_sucesso(passo_n + 1, pos_u, pos_v) | ||
if not ctrl_sucesso: | ||
self.limpa(pos_u, pos_v) | ||
if ctrl_sucesso: | ||
break | ||
return ctrl_sucesso | ||
|
||
def resolve_sucesso(self, passo_n, x, y): | ||
if passo_n - 1 < self.tamanho_tabuleiro ** 2: | ||
return self.tenta(passo_n, x, y) | ||
else: | ||
return True | ||
|
||
def posicao_valida(self, x, y): | ||
return self.dentro_do_tabuleiro(x, y) and self.posicao_vazia(x, y) | ||
|
||
def dentro_do_tabuleiro(self, x, y): | ||
return x in range(self.tamanho_tabuleiro) and y in range( | ||
self.tamanho_tabuleiro | ||
) | ||
|
||
def posicao_vazia(self, x, y): | ||
return not self.tabuleiro[x][y] | ||
|
||
def preenche(self, passo_n, x, y): | ||
self.tabuleiro[x][y] = passo_n | ||
|
||
def limpa(self, x, y): | ||
self.tabuleiro[x][y] = 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
p = PasseioCavalo(5) | ||
if p.tenta(2, 0, 0): | ||
pprint(p.tabuleiro) |