Created
January 22, 2011 20:51
Revisions
-
tuxes revised this gist
Jan 28, 2011 . 2 changed files with 53 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,44 +2,81 @@ # -*- encoding:utf-8 -*- class Matriz(object): """ Classe Matriz utilizada para resolver os 2 problemas do Dojo Data: 22/01/2011 Problema: Problema 28 do Projeto Euler http://projecteuler.net/index.php?section=problems&id=28 A função gera cria a matriz de ordem tam em espiral partindo do centro da Matriz A função soma_diagonais calcula a soma das diagonais da matriz: primária e secundária. """ def __init__(self): pass def gera(self, tam): """ A lógica usada para resolver o problema foi partir do último valor do espiral para o primeiro. O procedimento é fazendo um cobertura em formatos de L exceto para a primeira linha e as duas últimas posições. """ # a nossa matriz é uma lista de listas self.matriz = [] # inserindo a linha superior self.matriz.insert(0, range(tam*tam-tam+1, tam*tam+1)) # gerando as outas listas com qualquer valor for i in range(1,tam): self.matriz.append(range(0,tam)) # variáveis de controle x = tam*tam-tam linha = 1; coluna = 0 h = l = 1 # loop principal dos L's a serem preenchidos while(x >= 1): # parte vertical do L for loop in range(0,tam-1): self.matriz[linha][coluna] = x x -= 1 linha += l linha -= h coluna += l # parte horizontal do L for loop in range(0,tam-1): self.matriz[linha][coluna] = x x -= 1 coluna += l coluna -= l linha -= h # ajuste do L(direção e tamanho) tam -= 1 h *= -1 l *= -1 return self.matriz def soma_diagonais(self): """ Soma os valores das diagonais. i == j: diagonal principal i + j == self.ordem-1: diagonal secundária """ soma = 0 self.ordem = len(self.matriz) for i in range(0, self.ordem): for j in range(0, self.ordem): if i+j == self.ordem-1 or i == j: soma += self.matriz[i][j] return soma 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 charactersOriginal file line number Diff line number Diff line change @@ -6,9 +6,14 @@ from Matriz import * class problem28euler_TestCase(unittest.TestCase): """ Classe de teste """ def test_5x5(self): """ Teste da primeira parte do problema. Dada a matriz 5x5 encontrar a soma das diagonais. """ matriz = Matriz() matriz.matriz = [ [21, 22, 23, 24, 25], [20, 7, 8, 9, 10], @@ -18,6 +23,10 @@ def test_5x5(self): assert_equals(matriz.soma_diagonais(), 101) def test_se_gera_uma_matriz_5_por_5(self): """ Teste da segunda parte do problema. Dada a ordem da Matriz gerar a sua espiral. """ matriz = Matriz() ans = [ [21, 22, 23, 24, 25], [20, 7, 8, 9, 10], @@ -26,4 +35,5 @@ def test_se_gera_uma_matriz_5_por_5(self): [17, 16, 15, 14, 13]] assert_equals(matriz.gera(5), ans) if __name__ == '__main__': unittest.main() -
tuxes revised this gist
Jan 28, 2011 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
tuxes created this gist
Jan 22, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ #!/usr/bin/env python2.7 # -*- encoding:utf-8 -*- import unittest from nose.tools import * from Matriz import * class problem28euler_TestCase(unittest.TestCase): def test_5x5(self): matriz = Matriz() matriz.matriz = [ [21, 22, 23, 24, 25], [20, 7, 8, 9, 10], [19, 6, 1, 2, 11], [18, 5, 4, 3 ,12], [17, 16, 15, 14, 13]] assert_equals(matriz.soma_diagonais(), 101) def test_se_gera_uma_matriz_5_por_5(self): matriz = Matriz() ans = [ [21, 22, 23, 24, 25], [20, 7, 8, 9, 10], [19, 6, 1, 2, 11], [18, 5, 4, 3 ,12], [17, 16, 15, 14, 13]] assert_equals(matriz.gera(5), ans) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ #!/usr/bin/env python2.7 # -*- encoding:utf-8 -*- class Matriz(object): def __init__(self): pass def gera(self, tam): self.matriz = [] self.matriz.insert(0, range(tam*tam-tam+1, tam*tam+1)) for i in range(1,tam): self.matriz.append(range(0,tam)) x = tam*tam-tam linha = 1; coluna = 0 h = l = 1 while(x >= 1): for alph in range(0,tam-1): self.matriz[linha][coluna] = x x -= 1 linha += l linha -= h coluna += l for alph in range(0,tam-1): self.matriz[linha][coluna] = x x -= 1 coluna += l coluna -= l linha -= h tam -= 1 h *= -1 l *= -1 return self.matriz def soma_diagonais(self): soma = 0 self.ordem = len(self.matriz) for i in range(0, self.ordem): for j in range(0, self.ordem): if i+j == self.ordem-1 or i == j: soma += self.matriz[i][j] return soma