Skip to content

Add Flood Fill in Python #753

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
merged 7 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- James Goytia
- Amaras
- Jonathan Dönszelmann
- Ishaan Verma
105 changes: 105 additions & 0 deletions contents/flood_fill/code/python/flood_fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from collections import namedtuple
from queue import Queue

Point = namedtuple("Point", "x y")
Canvas = namedtuple("Canvas", "max_x max_y data")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is possible at all. An element of a tuple should probably be immutable itself if you want to avoid errors, while the way you implemented the algorithm is using a list.
Maybe you should consider using a dataclass instead, which doesn't have this restriction.


def inbounds(canvas, p):
if p.x < 0 or p.y < 0 or p.x >= canvas.max_x or p.y >= canvas.max_y:
return False
return True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a simple return statement like

def inbounds(canvas, p):
    return min(p) >= 0 and p.x < canvas.max_x and p.y < canvas.max_y


def color(canvas, p, new_val):
canvas.data[p.x][p.y] = new_val

def find_neighbors(canvas, p, old_val, new_val):
# north, south, east, west neighbors
possible_neighbors = [
Point(p.x, p.y+1),
Point(p.x+1, p.y),
Point(p.x-1, p.y),
Point(p.x, p.y-1)
]

# exclude the neighbors that go out of bounds and should not be colored
neighbors = []
for possible_neighbor in possible_neighbors:
if inbounds(canvas, possible_neighbor):
if canvas.data[possible_neighbor.x][possible_neighbor.y] == old_val:
neighbors.append(possible_neighbor)
return neighbors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if you would use a generator instead of returning a list, but it works well enough for 4 neighbours


def stack_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return

S = list()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python style guide recommends that variable names should be in snake_case. In particular, they should be in lower case.

S.append(p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you could just do s = [p], instead of creating an empty list and appending to it.


while len(S) > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python has the capability of evaluating the "thruthiness" of an object. So the loop condition simplifies to:

while s:

cur_loc = S.pop()
if canvas.data[cur_loc.x][cur_loc.y] == old_val:
color(canvas, cur_loc, new_val)
S+= find_neighbors(canvas, cur_loc, old_val, new_val)

def queue_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return

Q = Queue()
Q.put(p)

color(canvas, p, new_val)

while not Q.empty():
cur_loc = Q.get()
neighbors = find_neighbors(canvas, cur_loc, old_val, new_val)

for neighbor in neighbors:
color(canvas, neighbor, new_val)
Q.put(neighbor)

def recursive_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return

color(canvas, p, new_val)

neighbors = find_neighbors(canvas, p, old_val, new_val)
for neighbor in neighbors:
recursive_fill(canvas, neighbor, old_val, new_val)

def main():
grid = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
answer = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]

c0 = Canvas(5, 5, grid)
c1 = Canvas(5, 5, grid)
c2 = Canvas(5, 5, grid)

start_loc = Point(0, 0)

recursive_fill(c0, start_loc, 0, 1)
queue_fill(c1, start_loc, 0, 1)
stack_fill(c2, start_loc, 0, 1)

assert c0.data == answer
assert c1.data == answer
assert c2.data == answer

print("Tests Passed")

if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions contents/flood_fill/flood_fill.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ In code, this might look like this:
[import:37-55, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:34-52, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:15-30, lang="python"](code/python/flood_fill.py)
{% endmethod %}


Expand All @@ -106,6 +108,8 @@ In code, it might look like this:
[import:106-118, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:180-195, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:62-70, lang="python"](code/python/flood_fill.py)
{% endmethod %}

All code snippets for this chapter rely on an exterior `color` function, defined as
Expand All @@ -115,6 +119,8 @@ All code snippets for this chapter rely on an exterior `color` function, defined
[import:23-35, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:28-32, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:12-13, lang="python"](code/python/flood_fill.py)
{% endmethod %}

The above code continues recursing through available neighbors as long as neighbors exist, and this should work so long as we are adding the correct set of neighbors.
Expand All @@ -126,6 +132,8 @@ Additionally, it is possible to do the same type of traversal by managing a stac
[import:57-77, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:85-108, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:32-43, lang="python"](code/python/flood_fill.py)
{% endmethod %}

This is ultimately the same method of traversal as before; however, because we are managing our own data structure, there are a few distinct differences:
Expand Down Expand Up @@ -165,6 +173,8 @@ The code would look something like this:
[import:80-104, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:155-178, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:45-60, lang="python"](code/python/flood_fill.py)
{% endmethod %}

Now, there is a small trick in this code that must be considered to make sure it runs optimally.
Expand Down Expand Up @@ -243,6 +253,8 @@ After, we will fill in the left-hand side of the array to be all ones by choosin
[import, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import, lang:"c"](code/c/flood_fill.c)
{% sample lang="py" %}
[import:, lang="python"](code/python/flood_fill.py)
{% endmethod %}


Expand Down