Last active
October 4, 2016 03:51
-
-
Save wbadart/d2b5c1cddc7f51b40dabbdbf34a57c96 to your computer and use it in GitHub Desktop.
Simulate the "fill color" operation of programs like MS with lists.
This file contains 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
''' | |
' paint.py | |
' | |
' Simulate the "fill color" operation of | |
' programs like MS with lists. | |
' | |
' Will Badart | |
' SEP 2016 | |
' | |
''' | |
def paint(matrix, color, x, y): | |
current_color = matrix[x][y] | |
matrix[x][y] = color | |
if x - 1 >= 0 and matrix[x - 1][y] == current_color: | |
paint(matrix, color, x - 1, y) | |
if x + 1 < len(matrix) and matrix[x + 1][y] == current_color: | |
paint(matrix, color, x + 1, y) | |
if y - 1 >= 0 and matrix[x][y - 1] == current_color: | |
paint(matrix, color, x, y - 1) | |
if y + 1 < len(matrix[x]) and matrix[x][y + 1] == current_color: | |
paint(matrix, color, x, y + 1) | |
canvas = [[1, 1, 1, 1, 1], | |
[1, 2, 1, 1, 2], | |
[1, 2, 2, 1, 1], | |
[1, 1, 1, 1, 1]] | |
def print2d(matrix): | |
for row in matrix: | |
print row | |
print 'Original canvas:' | |
print2d(canvas) | |
paint(canvas, 3, 1, 1) | |
print 'Painted canvas:' | |
print2d(canvas) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment