Last active
December 14, 2019 12:20
-
-
Save johnberroa/ce0f6c6b2dabf6164d6ed8de9ad27af4 to your computer and use it in GitHub Desktop.
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
from copy import deepcopy | |
from typing import List | |
def propagate_color(matrix: List[List[str]], color: str) -> List[List[str]]: | |
"""Propagates color to all same colors that neighbor the color at [0,0] | |
Note: only works for square matrices! | |
""" | |
to_change: str = matrix[0][0] | |
changed_matrix = deepcopy(matrix) | |
for row in range(0, len(matrix)): | |
for column in range(0, len(matrix)): | |
if row == column == 0: | |
changed_matrix[row][column] = color | |
above = matrix[row - 1][column] | |
left = matrix[row][column - 1] | |
neighbors = above == to_change or left == to_change | |
cell = matrix[row][column] == to_change | |
if neighbors and cell: | |
changed_matrix[row][column] = color | |
return changed_matrix | |
def pretty_print(matrix: List[List[str]]) -> None: | |
for row in matrix: | |
print(row) | |
if __name__ == '__main__': | |
print("Starting test...") | |
matrix = [['R', 'B', 'R'], | |
['R', 'G', 'G'], | |
['R', 'R', 'B']] | |
print("Starting Matrix") | |
pretty_print(matrix) | |
print("Changing to color 'B'") | |
matrix = propagate_color(matrix, "B") | |
pretty_print(matrix) | |
print("Changing to color 'G'") | |
matrix = propagate_color(matrix, "G") | |
pretty_print(matrix) | |
print("\nComplete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment