-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Update 3n+1.py #996
Conversation
Made variable names more meaningful and removed nested functions.
What should happen on a negative number? a floating point number? def n31(initial_number):
"""
>>> n31(43) # doctest: +ELLIPSIS
([43, ...], 29)
""" |
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 |
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.
Would it also work to return path, len(path) ?
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.
Yes, that is more elegant, thanks!
maths/3n+1.py
Outdated
""" | ||
Returns Collatz sequence of number a | ||
|
||
Returns Collatz sequence of a number |
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 comment says that we are only returning the sequence but we are returning the sequence and its length.
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.
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)) |
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.
You can drop the 0, 1, and 2 here and drop the 0 in the raise statements above.
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.
Let's ship it.
* 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
Made variable names more meaningful and removed nested functions.