Skip to content

Instantly share code, notes, and snippets.

@thor27
Created March 31, 2018 01:40
Show Gist options
  • Save thor27/f238a3574fc43db45a30394efdad8108 to your computer and use it in GitHub Desktop.
Save thor27/f238a3574fc43db45a30394efdad8108 to your computer and use it in GitHub Desktop.
from time import sleep
import sys
start = 0
left = 1
right = 2
up = 3
down = 4
def print_screen(matrix, time=0.3, last_frame=False):
for linha in matrix:
for elemento in linha:
if elemento == 0:
print('░', end='')
else:
print('█', end='')
print('')
print('')
sleep(time)
if last_frame:
print('')
else:
sys.stdout.write('\r\033[{}A'.format(len(matrix)+1))
def paint(matrix, x, y):
print(
"\nMatrix size: {}x{}. Click point: {}x{}".format(
len(matrix),
len(matrix[0]),
x,
y
)
)
print_screen(matrix, 1)
do_paint(matrix, x, y)
print_screen(matrix, 1, True)
def do_paint(matrix, x, y, direction=start):
if matrix[y][x] == 1:
return
matrix[y][x] = 1
print_screen(matrix)
if direction != left and x < len(matrix[y])-1:
do_paint(matrix, x+1, y, direction=right)
if direction != right and x > 0:
do_paint(matrix, x-1, y, direction=left)
if direction != up and y < len(matrix)-1:
do_paint(matrix, x, y+1, direction=down)
if direction != down and y > 0:
do_paint(matrix, x, y-1, direction=up)
import unittest
from balde import *
def PrintName(func):
def teste(self):
print("\n=============================================================")
print(" ".join(func.__name__.split('_')))
print("=============================================================")
return func(self)
return teste
class TesteBalde(unittest.TestCase):
@PrintName
def test_0_paint_1x1_matrix(self):
matrix = [[0]]
paint(matrix, 0, 0)
self.assertEqual(matrix, [[1]])
@PrintName
def test_1_paint_nx1_matrix_one_block(self):
matrix = [[1, 1, 0, 1, 0]]
paint(matrix, 2, 0)
self.assertEqual(matrix, [[1, 1, 1, 1, 0]])
@PrintName
def test_2_paint_nxn_matrix_one_block(self):
matrix = [
[0, 1, 1, 1, 0],
[1, 1, 0, 1, 0],
[0, 1, 1, 1, 0]
]
paint(matrix, 2, 1)
self.assertEqual(matrix, [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 1, 1, 1, 0]
])
@PrintName
def test_3_paint_nx1_matrix_multipe_fw_blocks(self):
matrix = [[1, 0, 0, 1, 0]]
paint(matrix, 1, 0)
self.assertEqual(matrix, [[1, 1, 1, 1, 0]])
@PrintName
def test_4_paint_nx1_matrix_multipe_bw_blocks(self):
matrix = [[1, 0, 0, 1, 0]]
paint(matrix, 2, 0)
self.assertEqual(matrix, [[1, 1, 1, 1, 0]])
@PrintName
def test_5_does_not_paint_nx1_matrix_no_blocks(self):
matrix = [[1, 0, 1, 0, 0]]
paint(matrix, 2, 0)
self.assertEqual(matrix, [[1, 0, 1, 0, 0]])
@PrintName
def test_6_paint_nxn_matrix_one_next_block(self):
matrix = [
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0]
]
paint(matrix, 2, 1)
self.assertEqual(matrix, [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]
])
@PrintName
def test_paint_nxn_matrix_complex_case(self):
matrix = [
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1],
[0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1]
]
paint(matrix, 5, 3)
self.maxDiff = None
self.assertEqual(matrix, [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]
])
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment