-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
rename and add doctest #1501
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
rename and add doctest #1501
Conversation
maths/find_max.py
Outdated
@@ -1,16 +1,21 @@ | |||
# NguyenU |
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 file is out of sync with master
. There are already doctests on this file:
https://github.com/TheAlgorithms/Python/blob/master/maths/find_max.py#L6
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.
@cclauss Please review again!
Please write a https://docs.python.org/3/library/timeit.html that proves that the new implementation is faster than the current implementation. My bet is that the function call and slice overhead means that it is not. |
Did you mean below code implementation is slower ? max_num = nums[0]
for i in range(1, len(nums)):
if nums[i] > max_num:
max_num = nums[i] |
Not sure but that is my suspicion but a timeit test can tell us for sure. |
After I tested using def find_max(nums):
max_num = nums[0]
for i in range(1, len(nums)):
if nums[i] > max_num:
max_num = nums[i]
return max_num
print(timeit.timeit("find_max([2, 4, 9, 7, 19, 94, 5])", setup="from __main__ import find_max", number=10000))
time: 0.022231400000000002 def find_max(nums):
max = nums[0]
for x in nums:
if x > max:
max = x
return max
print(timeit.timeit("find_max([2, 4, 9, 7, 19, 94, 5])", setup="from __main__ import find_max", number=10000))
time: 0.007430200000000005 From the result, You are right. Thanks |
This is awesome work. I think we should fill this file with as many different implementations of find_max() as we can think of with timeit tests for each one in an effort to find that fastest pure Python implementation. How does |
|
maths/find_min.py
Outdated
|
||
>>> find_min([1, 3, 5, 7, 9]) == 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.
Let's use the same tests data as find_max.py uses.
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. Please review again
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.
Awesome work! Thanks for your persistence.
@cclauss