Skip to content

Instantly share code, notes, and snippets.

@ricardosiri68
Created July 21, 2015 20:39
Show Gist options
  • Save ricardosiri68/7b22d074e5cb8516990c to your computer and use it in GitHub Desktop.
Save ricardosiri68/7b22d074e5cb8516990c to your computer and use it in GitHub Desktop.
import sys
class Cuadrado:
char_a = '*'
char_b = ' '
def __init__(self, width):
self.width = width
self.height = width
def matrix(self):
pass
def draw(self):
return '\n'.join([''.join(row) for row in self.matrix()])
class CuadradoDisperso(Cuadrado):
def matrix(self):
sequence = True
for row in range(self.height):
args = (self.char_a, self.char_b) if sequence else\
(self.char_b, self.char_a)
yield self.sequence(*args)
sequence = not sequence
def sequence(self, first, second):
if not self.width % 2:
return self.width/2 * (first, second)
else:
return (self.width/2 * (first, second)) + (first,)
class CuadradoPerimetro(Cuadrado):
def matrix(self):
yield self.sequence()
for row in range(self.height - 2):
yield self.sequence(row)
yield self.sequence()
def sequence(self, row=None):
if row is not None:
output_row = (self.width - 2) * [self.char_b]
output_row[row] = self.char_a
return [self.char_a] + output_row + [self.char_a]
return self.width * [self.char_a]
class CuadradoDiagonal(Cuadrado):
def matrix(self):
for row in range(self.height):
yield self.sequence(row)
def sequence(self, row):
output_row = self.width * [self.char_b]
output_row[row] = self.char_a
return output_row
if __name__ == '__main__':
try:
size = int(sys.argv[1])
except ValueError:
raise Exception('El argumento del modulo debe ser un numero entero')
print 'CUADRADO DISPERSO'
print CuadradoDisperso(size).draw()
print 'CUADRADO PERIMETRADO DIAGONAL'
print CuadradoPerimetro(size).draw()
print 'CUADRADO DIAGONAL'
print CuadradoDiagonal(size).draw()
import cuadrados
import unittest
class TestCuadrados(unittest.TestCase):
def test_disperso_1(self):
control = [
('*', ' ', '*', ' ', '*'),
(' ', '*', ' ', '*', ' '),
('*', ' ', '*', ' ', '*'),
(' ', '*', ' ', '*', ' '),
('*', ' ', '*', ' ', '*')
]
cuadrado = cuadrados.CuadradoDisperso(5)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
def test_disperso_2(self):
control = [
('*', ' ', '*', ' ', '*', ' '),
(' ', '*', ' ', '*', ' ', '*'),
('*', ' ', '*', ' ', '*', ' '),
(' ', '*', ' ', '*', ' ', '*'),
('*', ' ', '*', ' ', '*', ' '),
(' ', '*', ' ', '*', ' ', '*')
]
cuadrado = cuadrados.CuadradoDisperso(6)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
def test_perimetro_1(self):
control = [
['*', '*', '*', '*', '*'],
['*', '*', ' ', ' ', '*'],
['*', ' ', '*', ' ', '*'],
['*', ' ', ' ', '*', '*'],
['*', '*', '*', '*', '*']]
cuadrado = cuadrados.CuadradoPerimetro(5)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
def test_perimetro_2(self):
control = [
['*', '*', '*', '*', '*', '*'],
['*', '*', ' ', ' ', ' ', '*'],
['*', ' ', '*', ' ', ' ', '*'],
['*', ' ', ' ', '*', ' ', '*'],
['*', ' ', ' ', ' ', '*', '*'],
['*', '*', '*', '*', '*', '*']
]
cuadrado = cuadrados.CuadradoPerimetro(6)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
def test_diagonal_1(self):
control = [
['*', ' ', ' ', ' ', ' '],
[' ', '*', ' ', ' ', ' '],
[' ', ' ', '*', ' ', ' '],
[' ', ' ', ' ', '*', ' '],
[' ', ' ', ' ', ' ', '*']
]
cuadrado = cuadrados.CuadradoDiagonal(5)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
def test_diagonal_2(self):
control = [
['*', ' ', ' ', ' ', ' ', ' '],
[' ', '*', ' ', ' ', ' ', ' '],
[' ', ' ', '*', ' ', ' ', ' '],
[' ', ' ', ' ', '*', ' ', ' '],
[' ', ' ', ' ', ' ', '*', ' '],
[' ', ' ', ' ', ' ', ' ', '*']
]
cuadrado = cuadrados.CuadradoDiagonal(6)
matrix = list(cuadrado.matrix())
self.assertListEqual(control, matrix)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment