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): | |
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 |
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): | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment