-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Merged
berquist
merged 4 commits into
algorithm-archivists:master
from
Amaras:coconut_flood_fill
Sep 6, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
408ca92
Re-added flood fill in Coconut (without the stupid master merge)
Amaras 225bd1c
Removed the 'colour' function, reworked the md, and added a test func…
Amaras 6643539
taken berquist's review into account
Amaras a8e5cbf
removed redundant new_value in find_neighbours
Amaras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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): | ||
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)) | ||
|
||
|
||
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): | ||
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) | ||
|> map$(recursive_fill$(canvas, ?, old_value, new_value)) | ||
) | ||
|
||
|
||
def test_grid(initial_canvas, final_canvas, function): | ||
canvas = initial_canvas.copy() # ensure the initial_canvas is unchanged | ||
function(canvas) | ||
return (canvas == final_canvas).all() | ||
|
||
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) | ||
|
||
recursive_test_func = recursive_fill$(?, starting_location, 0, 1) | ||
# The following is manual unit testing of the function | ||
if test_grid(grid, solution_grid, recursive_test_func): | ||
pass_count += 1 | ||
print('.', end='') | ||
else: | ||
failure_count += 1 | ||
print('F', end='') | ||
|
||
stack_test_func = stack_fill$(?, starting_location, 0, 1) | ||
if test_grid(grid, solution_grid, stack_test_func): | ||
print('.', end='') | ||
pass_count += 1 | ||
else: | ||
print('F', end='') | ||
failure_count += 1 | ||
|
||
queue_test_func = queue_fill$(?, starting_location, 0, 1) | ||
if test_grid(grid, solution_grid, queue_test_func): | ||
print('.', end='') | ||
pass_count += 1 | ||
else: | ||
print('F', end='') | ||
failure_count += 1 | ||
|
||
print() | ||
print(TestResults(pass_count, failure_count)) | ||
|
||
if __name__ == '__main__': | ||
# Testing setup | ||
test() | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely, I think we'll just have to remove the
test
function for standardization.