Skip to content

Implementation of the algorithm for the Koch snowflake #4207

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
Feb 20, 2021
Merged

Implementation of the algorithm for the Koch snowflake #4207

merged 7 commits into from
Feb 20, 2021

Conversation

algobytewise
Copy link
Contributor

Implementation of the algorithm for the Koch snowflake

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Implementation of the algorithm for the Koch snowflake
@ghost ghost added the require tests Tests [doctest/unittest/pytest] are required label Feb 15, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



# initial triangle of Koch snowflake
VECTOR1 = numpy.array([0, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR1


# initial triangle of Koch snowflake
VECTOR1 = numpy.array([0, 0])
VECTOR2 = numpy.array([0.5, 0.8660254])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR2

# initial triangle of Koch snowflake
VECTOR1 = numpy.array([0, 0])
VECTOR2 = numpy.array([0.5, 0.8660254])
VECTOR3 = numpy.array([1, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR3

return numpy.dot(rotation_matrix, vector)


def plot(vectors: list[numpy.ndarray]) -> None:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/koch_snowflake.py, please provide doctest for the function plot

@ghost ghost added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Feb 15, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_1


# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
VECTOR_2 = numpy.array([0.5, 0.8660254])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_2

# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
VECTOR_2 = numpy.array([0.5, 0.8660254])
VECTOR_3 = numpy.array([1, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_3

return numpy.dot(rotation_matrix, vector)


def plot(vectors: list[numpy.ndarray]) -> None:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/koch_snowflake.py, please provide doctest for the function plot

@algobytewise
Copy link
Contributor Author

algobytewise commented Feb 15, 2021

Hello, it would be great if I could get some feedback on how to respond to the failed tests.
(1) algorithms-keeper complains that VECTOR_1, VECTOR_2 and VECTOR_3 are not snake_case. But they are intended as constants, not as variables.
(2) I'm not sure what doctest to implement for the plot-function. It is not part of the algorithm, it just draws a graph and it has no return value.

@mrmaxguns
Copy link
Member

The bot just gives guidance. Tests failed because you didn't run isort. Here's the isort diff:

 from __future__ import annotations
-import numpy
 
+import numpy
 
 # initial triangle of Koch snowflake
 VECTOR_1 = numpy.array([0, 0])

As you can see, a newline was added between from __future__ import annotations and import numpy. The constants are fine. For the tests, you should probably add a small note in the relevant function why you chose not to include tests, so that future contributors know.

I fixed the sorting of the imports and I added a comment to the plot-function to explain what it does and why it doesn't use a doctest. Thank you to user mrmaxguns for suggesting these changes.
@ghost ghost removed the require tests Tests [doctest/unittest/pytest] are required label Feb 20, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Utility function to plot the vectors using matplotlib.pyplot
No doctest was implemented since this function does not have a return value
"""
import matplotlib.pyplot.
Copy link

Choose a reason for hiding this comment

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

An error occured while parsing the file: other/koch_snowflake.py

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/libcst/_parser/base_parser.py", line 152, in _add_token
    plan = stack[-1].dfa.transitions[transition]
KeyError: TokenType(NEWLINE)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/algorithms_keeper/parser/python_parser.py", line 145, in parse
    reports = lint_file(
libcst._exceptions.ParserSyntaxError: Syntax Error @ 99:30.
Incomplete input. Encountered '\r\n', but expected 'NAME'.

    import matplotlib.pyplot.
                             ^

@ghost ghost added the require tests Tests [doctest/unittest/pytest] are required label Feb 20, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

import numpy

# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_1


# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
VECTOR_2 = numpy.array([0.5, 0.8660254])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_2

# initial triangle of Koch snowflake
VECTOR_1 = numpy.array([0, 0])
VECTOR_2 = numpy.array([0.5, 0.8660254])
VECTOR_3 = numpy.array([1, 0])
Copy link

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: VECTOR_3

return numpy.dot(rotation_matrix, vector)


def plot(vectors: list[numpy.ndarray]) -> None:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/koch_snowflake.py, please provide doctest for the function plot

@ghost ghost removed the tests are failing Do not merge until tests pass label Feb 20, 2021
@algobytewise
Copy link
Contributor Author

Dear mrmaxguns, thank you for the feedback, I have implemented the changes you suggested.

@cclauss
Copy link
Member

cclauss commented Feb 20, 2021

This looks good. Nice work. I am not a fan of uncategorized algorithms in the other directory because it makes it harder for visitors to find algorithms of interest. Would it make sense to move this to the cellular_automata or graphics directory instead?

Comment on lines 106 to 111
x_coordinates = []
for vector in vectors:
x_coordinates.append(vector[0])
y_coordinates = []
for vector in vectors:
y_coordinates.append(vector[1])
Copy link
Member

Choose a reason for hiding this comment

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

Please make these list comprehensions.

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe even slicker, use zip() instead.

0.28867513]), array([0.66666667, 0. ]), array([1, 0])]
"""
new_vectors = []
for i in range(len(vectors) - 1):
Copy link
Member

Choose a reason for hiding this comment

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

range(len()) is almost always a sign that enumerate() could be used instead.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

return numpy.dot(rotation_matrix, vector)


def plot(vectors: list[numpy.ndarray]) -> None:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file graphics/koch_snowflake.py, please provide doctest for the function plot

@algobytewise
Copy link
Contributor Author

I have implemented the changes, thanks for the advice on better looping. I think graphics is a better fit so I've put it there. My preferred solution would be to have a new directory for fractals and move it there together with other/sierpinski_triangle.py.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

return numpy.dot(rotation_matrix, vector)


def plot(vectors: list[numpy.ndarray]) -> None:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file graphics/koch_snowflake.py, please provide doctest for the function plot

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Feb 20, 2021
@cclauss cclauss merged commit 6bb9a02 into TheAlgorithms:master Feb 20, 2021
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
…#4207)

* Add files via upload

Implementation of the algorithm for the Koch snowflake

* added underscore to variable names

* added newline and comment

I fixed the sorting of the imports and I added a comment to the plot-function to explain what it does and why it doesn't use a doctest. Thank you to user mrmaxguns for suggesting these changes.

* fixed accidental newline in the middle of expression

* improved looping

* moved "koch_snowflake.py" from "other" to "graphics"

* Update koch_snowflake.py

Co-authored-by: Christian Clauss <[email protected]>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
…#4207)

* Add files via upload

Implementation of the algorithm for the Koch snowflake

* added underscore to variable names

* added newline and comment

I fixed the sorting of the imports and I added a comment to the plot-function to explain what it does and why it doesn't use a doctest. Thank you to user mrmaxguns for suggesting these changes.

* fixed accidental newline in the middle of expression

* improved looping

* moved "koch_snowflake.py" from "other" to "graphics"

* Update koch_snowflake.py

Co-authored-by: Christian Clauss <[email protected]>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
…#4207)

* Add files via upload

Implementation of the algorithm for the Koch snowflake

* added underscore to variable names

* added newline and comment

I fixed the sorting of the imports and I added a comment to the plot-function to explain what it does and why it doesn't use a doctest. Thank you to user mrmaxguns for suggesting these changes.

* fixed accidental newline in the middle of expression

* improved looping

* moved "koch_snowflake.py" from "other" to "graphics"

* Update koch_snowflake.py

Co-authored-by: Christian Clauss <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
require tests Tests [doctest/unittest/pytest] are required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants