From 681f94ffa69cebca9ddfc0f4c080af269a96b4f5 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:38:07 -0300 Subject: [PATCH 1/8] =?UTF-8?q?2.1=20-=20Implementa=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/1_arvore_central.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 2_paradigmas/1_arvore_central.py diff --git a/2_paradigmas/1_arvore_central.py b/2_paradigmas/1_arvore_central.py new file mode 100644 index 0000000..c1c3c62 --- /dev/null +++ b/2_paradigmas/1_arvore_central.py @@ -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 From 37a355fd432aed5983c8cb2d6e3a6bf3293b005b Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:38:58 -0300 Subject: [PATCH 2/8] =?UTF-8?q?2.2=20-=20Decis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/2_central.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 2_paradigmas/2_central.py diff --git a/2_paradigmas/2_central.py b/2_paradigmas/2_central.py new file mode 100644 index 0000000..57cda1c --- /dev/null +++ b/2_paradigmas/2_central.py @@ -0,0 +1,3 @@ +""" +Implementado no arquivo 1_arvore_central.py +""" \ No newline at end of file From fe2fa17c03361bad4a51c1314e02818b7e9bf602 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:39:16 -0300 Subject: [PATCH 3/8] 2.2 - Lint typo --- 2_paradigmas/2_central.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_paradigmas/2_central.py b/2_paradigmas/2_central.py index 57cda1c..76793ee 100644 --- a/2_paradigmas/2_central.py +++ b/2_paradigmas/2_central.py @@ -1,3 +1,3 @@ """ Implementado no arquivo 1_arvore_central.py -""" \ No newline at end of file +""" From 479791d6f13d7c1eeddfb64f8cc4738716968456 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:59:03 -0300 Subject: [PATCH 4/8] =?UTF-8?q?2.3=20-=20Implementa=C3=A7=C3=A3o=20e=20ass?= =?UTF-8?q?erts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/3_fibonacci.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2_paradigmas/3_fibonacci.py diff --git a/2_paradigmas/3_fibonacci.py b/2_paradigmas/3_fibonacci.py new file mode 100644 index 0000000..aad003c --- /dev/null +++ b/2_paradigmas/3_fibonacci.py @@ -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 From 3c78241991be82a00995e681fc61ebb3fff29d8b Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 12:00:23 -0300 Subject: [PATCH 5/8] =?UTF-8?q?2.4=20-=20Decis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/4_fibonacci.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 2_paradigmas/4_fibonacci.py diff --git a/2_paradigmas/4_fibonacci.py b/2_paradigmas/4_fibonacci.py new file mode 100644 index 0000000..5f63791 --- /dev/null +++ b/2_paradigmas/4_fibonacci.py @@ -0,0 +1,3 @@ +""" +Implementado no arquivo 3_fibonacci.py +""" From 709df6075a6c6db9a54a8908e810252efc27a247 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sun, 4 Apr 2021 19:40:11 -0300 Subject: [PATCH 6/8] =?UTF-8?q?2.5=20-=20Decis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/5_passeio_cavalo.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 2_paradigmas/5_passeio_cavalo.py diff --git a/2_paradigmas/5_passeio_cavalo.py b/2_paradigmas/5_passeio_cavalo.py new file mode 100644 index 0000000..85eb959 --- /dev/null +++ b/2_paradigmas/5_passeio_cavalo.py @@ -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. +""" From af5f1845be3e5b630d48a492ff95305ce3876b18 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sun, 4 Apr 2021 19:44:44 -0300 Subject: [PATCH 7/8] =?UTF-8?q?2.6=20-=20Implementa=C3=A7=C3=A3o=20e=20pri?= =?UTF-8?q?nt=20(tenta=20e=20tenta2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_paradigmas/6_passeio_cavalo.py | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 2_paradigmas/6_passeio_cavalo.py diff --git a/2_paradigmas/6_passeio_cavalo.py b/2_paradigmas/6_passeio_cavalo.py new file mode 100644 index 0000000..2eea9ea --- /dev/null +++ b/2_paradigmas/6_passeio_cavalo.py @@ -0,0 +1,89 @@ +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 tenta2(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.tenta2(passo_n, x, y) + else: + return True + + def tenta(self, passo_n, pos_x, pos_y): + selec_movimento = -1 + while True: + ctrl_sucesso = False + selec_movimento += 1 + direcao = self.PASSOS[selec_movimento] + pos_u = pos_x + direcao[0] + pos_v = pos_y + direcao[1] + + if self.dentro_do_tabuleiro(pos_u, pos_v): + if self.posicao_vazia(pos_u, pos_v): + self.preenche(passo_n, pos_u, pos_v) + if passo_n < self.tamanho_tabuleiro ** 2: + ctrl_sucesso = self.tenta(passo_n + 1, pos_u, pos_v) + if not ctrl_sucesso: + self.limpa(pos_u, pos_v) + else: + ctrl_sucesso = True + if not (not ctrl_sucesso and (selec_movimento != 7)): + break + return ctrl_sucesso + + 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.tenta2(2, 0, 0): + pprint(p.tabuleiro) From 6d4930059b2abdfb664e7a910fe3d95d5aae9d84 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sun, 4 Apr 2021 19:47:30 -0300 Subject: [PATCH 8/8] 2.6 - Remove tenta, deixa tenta2 como principal --- 2_paradigmas/6_passeio_cavalo.py | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/2_paradigmas/6_passeio_cavalo.py b/2_paradigmas/6_passeio_cavalo.py index 2eea9ea..a7c7e63 100644 --- a/2_paradigmas/6_passeio_cavalo.py +++ b/2_paradigmas/6_passeio_cavalo.py @@ -21,7 +21,7 @@ def __init__(self, tamanho_tabuleiro, inicio=(0, 0)): ] self.tabuleiro[inicio[0]][inicio[1]] = 1 - def tenta2(self, passo_n, pos_x, pos_y): + def tenta(self, passo_n, pos_x, pos_y): for direcao in self.PASSOS: ctrl_sucesso = False @@ -39,32 +39,10 @@ def tenta2(self, passo_n, pos_x, pos_y): def resolve_sucesso(self, passo_n, x, y): if passo_n - 1 < self.tamanho_tabuleiro ** 2: - return self.tenta2(passo_n, x, y) + return self.tenta(passo_n, x, y) else: return True - def tenta(self, passo_n, pos_x, pos_y): - selec_movimento = -1 - while True: - ctrl_sucesso = False - selec_movimento += 1 - direcao = self.PASSOS[selec_movimento] - pos_u = pos_x + direcao[0] - pos_v = pos_y + direcao[1] - - if self.dentro_do_tabuleiro(pos_u, pos_v): - if self.posicao_vazia(pos_u, pos_v): - self.preenche(passo_n, pos_u, pos_v) - if passo_n < self.tamanho_tabuleiro ** 2: - ctrl_sucesso = self.tenta(passo_n + 1, pos_u, pos_v) - if not ctrl_sucesso: - self.limpa(pos_u, pos_v) - else: - ctrl_sucesso = True - if not (not ctrl_sucesso and (selec_movimento != 7)): - break - return ctrl_sucesso - def posicao_valida(self, x, y): return self.dentro_do_tabuleiro(x, y) and self.posicao_vazia(x, y) @@ -85,5 +63,5 @@ def limpa(self, x, y): if __name__ == "__main__": p = PasseioCavalo(5) - if p.tenta2(2, 0, 0): + if p.tenta(2, 0, 0): pprint(p.tabuleiro)