-
-
Notifications
You must be signed in to change notification settings - Fork 360
Flood fill in Coconut #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
408ca92
225bd1c
6643539
a8e5cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from collections import deque | ||
import numpy as np | ||
|
||
|
||
data Point(x, y): | ||
def __add__(self, other is Point) = Point(self.x + other.x, self.y + other.y) | ||
|
||
|
||
# This function is necessary, because negative indices wrap around the | ||
# array in Coconut. | ||
def inbounds(canvas_shape, location is Point) = | ||
min(location) >= 0 and location.x < canvas_shape[0] and location.y < canvas_shape[1] | ||
|
||
|
||
def find_neighbours(canvas, location is Point, old_value, new_value): | ||
possible_neighbours = ((Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)) | ||
|> map$(location.__add__)) | ||
|
||
yield from possible_neighbours |> filter$(x -> (inbounds(canvas.shape, x) | ||
and canvas[x] == old_value)) | ||
|
||
|
||
def stack_fill(canvas, location is Point, old_value, new_value): | ||
if new_value == old_value or not inbounds(canvas.shape, location): | ||
return | ||
|
||
stack = [location] | ||
|
||
while stack: | ||
current_location = stack.pop() | ||
if canvas[current_location] == old_value: | ||
canvas[current_location] = new_value | ||
stack.extend(find_neighbours(canvas, current_location, old_value, | ||
new_value)) | ||
|
||
|
||
def queue_fill(canvas, location is Point, old_value, new_value): | ||
if new_value == old_value or not inbounds(canvas.shape, location): | ||
return | ||
|
||
queue = deque() | ||
queue.append(location) | ||
|
||
canvas[location] = new_value | ||
|
||
while queue: | ||
current_location = queue.popleft() | ||
for neighbour in find_neighbours(canvas, current_location, old_value, | ||
new_value): | ||
canvas[neighbour] = new_value | ||
queue.append(neighbour) | ||
|
||
|
||
def recursive_fill(canvas, location is Point, old_value, new_value): | ||
if new_value == old_value or not inbounds(canvas.shape, location): | ||
return | ||
|
||
canvas[location] = new_value | ||
# consume is important here, because otherwise, the recursive function is not called again | ||
consume( | ||
find_neighbours(canvas, location, old_value, new_value) | ||
|> map$(recursive_fill$(canvas, ?, old_value, new_value)) | ||
) | ||
|
||
def test(): | ||
from collections import namedtuple | ||
|
||
TestResults = namedtuple('TestResults', 'passes failures') | ||
pass_count = failure_count = 0 | ||
|
||
grid = np.zeros((5, 5)) | ||
grid[2,:] = 1 | ||
solution_grid = np.zeros((5, 5)) | ||
solution_grid[:3,] = 1 | ||
|
||
starting_location = Point(0, 0) | ||
|
||
|
||
# The following is manual unit testing of the function | ||
recursive_fill(grid, starting_location, 0, 1) | ||
try: | ||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert (grid == solution_grid).all() | ||
except AssertionError: | ||
print('F', end='') | ||
failure_count += 1 | ||
else: | ||
print('.', end='') | ||
pass_count += 1 | ||
|
||
# Resetting the grid, if everything went well. | ||
grid[:2,] = 0 | ||
|
||
stack_fill(grid, starting_location, 0, 1) | ||
try: | ||
assert (grid == solution_grid).all() | ||
except AssertionError: | ||
print('F', end='') | ||
failure_count += 1 | ||
else: | ||
print('.', end='') | ||
pass_count += 1 | ||
|
||
grid[:2,] = 0 | ||
|
||
queue_fill(grid, starting_location, 0, 1) | ||
try: | ||
assert (grid == solution_grid).all() | ||
except AssertionError: | ||
print('F', end='') | ||
failure_count += 1 | ||
else: | ||
print('.', end='') | ||
pass_count += 1 | ||
|
||
print('') | ||
print(TestResults(pass_count, failure_count)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice touch, but will probably have to be reworked when output standardization is done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, I think we'll just have to remove the |
||
|
||
if __name__ == '__main__': | ||
# Testing setup | ||
test() | ||
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.