Skip to content

Commit

Permalink
Merge pull request #10 from vbuxbaum/cap-paradigmas
Browse files Browse the repository at this point in the history
Cap paradigmas - Implementações 1 a 6
  • Loading branch information
vbuxbaum authored Apr 4, 2021
2 parents 8d486f8 + 6d49300 commit 60da077
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 2_paradigmas/1_arvore_central.py
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
3 changes: 3 additions & 0 deletions 2_paradigmas/2_central.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Implementado no arquivo 1_arvore_central.py
"""
24 changes: 24 additions & 0 deletions 2_paradigmas/3_fibonacci.py
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
3 changes: 3 additions & 0 deletions 2_paradigmas/4_fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Implementado no arquivo 3_fibonacci.py
"""
4 changes: 4 additions & 0 deletions 2_paradigmas/5_passeio_cavalo.py
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.
"""
67 changes: 67 additions & 0 deletions 2_paradigmas/6_passeio_cavalo.py
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)

0 comments on commit 60da077

Please sign in to comment.