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

Add Flood Fill in Python #753

merged 7 commits into from
Oct 12, 2020

Conversation

lazyprop
Copy link
Contributor

No description provided.

Copy link
Member

@Amaras Amaras left a comment

Choose a reason for hiding this comment

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

Hi, so this PR finally has a review.
There are several little problems that should be addressed, not least of which is the use of a namedtuple for representing your canvas.
Overall, I think it's a good addition to the AAA, though.

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

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

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.

return

S = list()
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.

S = list()
S.append(p)

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:

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.

@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Oct 3, 2020
Copy link
Member

@Amaras Amaras left a comment

Choose a reason for hiding this comment

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

This now looks good enough. I have nothing against it now.

Copy link
Member

@leios leios left a comment

Choose a reason for hiding this comment

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

This PR looks good, a few line numbers need to be fixed!

@leios leios merged commit 627aed9 into algorithm-archivists:master Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants