-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Add Flood Fill in Python #753
Conversation
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.
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 |
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 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 |
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.
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() |
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.
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) |
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.
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: |
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.
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") |
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.
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.
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 now looks good enough. I have nothing against it now.
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 PR looks good, a few line numbers need to be fixed!
No description provided.