-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Update sum_of_digits.py #2319
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 sum_of_digits.py #2319
Conversation
maths/sum_of_digits.py
Outdated
if __name__ == "__main__": | ||
print(sum_of_digits(12345)) # ===> 15 | ||
main() |
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.
Why remove this line?
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.
Already test using doctest
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.
Please add a timeit (or similar) benchmark to demonstrate which algorithm is faster.
def sum_of_digits_compact(n: int) -> int:
return sum(int(c) for c in str(n).lstrip("-")) |
Thanks. |
* add different version
Please review again. I have added benchmark @cclauss
|
* * support negative number * add different version * fixup! Format Python code with psf/black push * sum(int(c) for c in str(abs(n))) * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.