Created
January 22, 2011 20:51
-
-
Save tuxes/791461 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python2.7 | |
# -*- 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 characters
#!/usr/bin/env python2.7 | |
# -*- encoding:utf-8 -*- | |
import unittest | |
from nose.tools import * | |
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], | |
[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): | |
""" | |
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], | |
[19, 6, 1, 2, 11], | |
[18, 5, 4, 3 ,12], | |
[17, 16, 15, 14, 13]] | |
assert_equals(matriz.gera(5), ans) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment