Skip to content

Update 3n+1.py #996

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 4 commits into from
Jul 13, 2019
Merged

Update 3n+1.py #996

merged 4 commits into from
Jul 13, 2019

Conversation

SandersLin
Copy link
Contributor

Made variable names more meaningful and removed nested functions.

Made variable names more meaningful and removed nested functions.
@cclauss
Copy link
Member

cclauss commented Jul 10, 2019

What should happen on a negative number? a floating point number?

def n31(initial_number):
    """
    >>> n31(43)  # doctest: +ELLIPSIS
    ([43, ...], 29)
    """

@SandersLin
Copy link
Contributor Author

Good catch! Added argument validation and type hints.

maths/3n+1.py Outdated
@@ -15,10 +21,10 @@ def n31(a):
a = 3*a +1
counter += 1
path += [a]
return path , counter
return path, counter + 1
Copy link
Member

Choose a reason for hiding this comment

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

Would it also work to return path, len(path) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is more elegant, thanks!

maths/3n+1.py Outdated
"""
Returns Collatz sequence of number a

Returns Collatz sequence of a number
Copy link
Member

Choose a reason for hiding this comment

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

This comment says that we are only returning the sequence but we are returning the sequence and its length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated docstring

def main():
num = 4
path , length = n31(num)
print("The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(num,length, path))
Copy link
Member

Choose a reason for hiding this comment

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

You can drop the 0, 1, and 2 here and drop the 0 in the raise statements above.

@cclauss cclauss self-requested a review July 13, 2019 07:04
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.

Let's ship it.

@cclauss cclauss merged commit 1e0b33d into TheAlgorithms:master Jul 13, 2019
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Update 3n+1.py

Made variable names more meaningful and removed nested functions.

* Update 3n+1.py

* Update 3n+1.py

* Update 3n+1.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants