Reimplementation of min() in PythonReimplementation of C++ vectorSize improvements for cat reimplementationReimplementation of a Hashtable and hash functionReimplementation of Underscore _.throttleReimplementation of Laravel's config systemReimplementation of Python's dequeOptimisation suggestions for Min HeapMin/Max Heap implementation in PythonReimplementation of call() method in JavaScriptMin Heap implementation [C++]
Is it possible to grow new organs through exposure to radioactivity?
Are there any other rule mechanics that could grant Thieves' Cant?
If I animate and control a zombie, does it benefit from Undead Fortitude when it's reduced to 0 HP?
Do Reform Jews believe in a theistic God?
How should I write this passage to make it the most readable?
Can a bald person be a Nazir?
What is a good class if we remove subclasses?
Why did Saruman lie?
Markov-chain sentence generator in Python
How much can I judge a company based on a phone screening?
Are employers legally allowed to pay employees in goods and services equal to or greater than the minimum wage?
Does EU compensation apply to flights where the departure airport closes check-in counters during protests?
A torrent of foreign terms
Are there really no countries that protect Freedom of Speech as the United States does?
Submitting a new paper just after another was accepted by the same journal
How would timezones work on a planet 100 times the size of our Earth
Are those flyers about apartment purchase a scam?
Case Condition for two lines
Can the IPA represent all languages' tones?
Installing Windows to flash BIOS, then reinstalling Ubuntu
If you know the location of an invisible creature, can you attack it?
Why is statically linking glibc discouraged?
Why did IBM make public the PC BIOS source code?
How would you translate this? バタコチーズライス
Reimplementation of min() in Python
Reimplementation of C++ vectorSize improvements for cat reimplementationReimplementation of a Hashtable and hash functionReimplementation of Underscore _.throttleReimplementation of Laravel's config systemReimplementation of Python's dequeOptimisation suggestions for Min HeapMin/Max Heap implementation in PythonReimplementation of call() method in JavaScriptMin Heap implementation [C++]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'm reimplementing the min()
function as an exercise (EDIT: not all the functionality of the python std library function, just the minimum of a list of numbers). Here is my code:
def my_min(num_list):
minimum = num_list[0]
for num in num_list[1:]:
if num < minimum:
minimum = num
return minimum
My question is: How bad is num_list[1:]
in the for loop? And are there any other optimizations I could make to the code?
My intention by truncating the list is to avoid comparing the list's first element to itself. While insignificant in terms of wasted time and resources, I just find it lacking elegance.
python reinventing-the-wheel
$endgroup$
add a comment |
$begingroup$
I'm reimplementing the min()
function as an exercise (EDIT: not all the functionality of the python std library function, just the minimum of a list of numbers). Here is my code:
def my_min(num_list):
minimum = num_list[0]
for num in num_list[1:]:
if num < minimum:
minimum = num
return minimum
My question is: How bad is num_list[1:]
in the for loop? And are there any other optimizations I could make to the code?
My intention by truncating the list is to avoid comparing the list's first element to itself. While insignificant in terms of wasted time and resources, I just find it lacking elegance.
python reinventing-the-wheel
$endgroup$
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
$begingroup$
I'm reimplementing the min()
function as an exercise (EDIT: not all the functionality of the python std library function, just the minimum of a list of numbers). Here is my code:
def my_min(num_list):
minimum = num_list[0]
for num in num_list[1:]:
if num < minimum:
minimum = num
return minimum
My question is: How bad is num_list[1:]
in the for loop? And are there any other optimizations I could make to the code?
My intention by truncating the list is to avoid comparing the list's first element to itself. While insignificant in terms of wasted time and resources, I just find it lacking elegance.
python reinventing-the-wheel
$endgroup$
I'm reimplementing the min()
function as an exercise (EDIT: not all the functionality of the python std library function, just the minimum of a list of numbers). Here is my code:
def my_min(num_list):
minimum = num_list[0]
for num in num_list[1:]:
if num < minimum:
minimum = num
return minimum
My question is: How bad is num_list[1:]
in the for loop? And are there any other optimizations I could make to the code?
My intention by truncating the list is to avoid comparing the list's first element to itself. While insignificant in terms of wasted time and resources, I just find it lacking elegance.
python reinventing-the-wheel
python reinventing-the-wheel
edited 5 hours ago


200_success
135k21 gold badges173 silver badges443 bronze badges
135k21 gold badges173 silver badges443 bronze badges
asked 8 hours ago
jeremy radcliffjeremy radcliff
2502 silver badges9 bronze badges
2502 silver badges9 bronze badges
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
1
1
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
iterators
You can use an iterator
def my_min(num_list):
# empty lists
if not num_list:
raise ValueError('Empty list')
list_iter = iter(num_list)
minimum = next(list_iter)
for num in list_iter:
if num < minimum:
minimum = num
return minimum
In response to Mathias' comment, here is a version that works with an iterable:
def my_min(seq):
seq = iter(seq)
try:
minimum = next(seq)
for num in seq:
if num < minimum:
minimum = num
return minimum
except StopIteration as e:
pass
raise ValueError('Empty list')
$endgroup$
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around thenext
call, you could handle all kind of iterables, not only lists.
$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
add a comment |
$begingroup$
Review
I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
- You have implemented a simple function, so it shouldn't have been that hard to provide a couple of unit tests. You would have immediately found bugs on the most obvious edge cases as (1) empty list and self-created edge case (2) single item.
While insignificant in terms of wasted time and resources, I just
find it lacking elegance.
- What you gain in elegance is lost by the edge case guards you'd have to build in to fix the bugs.
$endgroup$
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226060%2freimplementation-of-min-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
iterators
You can use an iterator
def my_min(num_list):
# empty lists
if not num_list:
raise ValueError('Empty list')
list_iter = iter(num_list)
minimum = next(list_iter)
for num in list_iter:
if num < minimum:
minimum = num
return minimum
In response to Mathias' comment, here is a version that works with an iterable:
def my_min(seq):
seq = iter(seq)
try:
minimum = next(seq)
for num in seq:
if num < minimum:
minimum = num
return minimum
except StopIteration as e:
pass
raise ValueError('Empty list')
$endgroup$
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around thenext
call, you could handle all kind of iterables, not only lists.
$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
add a comment |
$begingroup$
iterators
You can use an iterator
def my_min(num_list):
# empty lists
if not num_list:
raise ValueError('Empty list')
list_iter = iter(num_list)
minimum = next(list_iter)
for num in list_iter:
if num < minimum:
minimum = num
return minimum
In response to Mathias' comment, here is a version that works with an iterable:
def my_min(seq):
seq = iter(seq)
try:
minimum = next(seq)
for num in seq:
if num < minimum:
minimum = num
return minimum
except StopIteration as e:
pass
raise ValueError('Empty list')
$endgroup$
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around thenext
call, you could handle all kind of iterables, not only lists.
$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
add a comment |
$begingroup$
iterators
You can use an iterator
def my_min(num_list):
# empty lists
if not num_list:
raise ValueError('Empty list')
list_iter = iter(num_list)
minimum = next(list_iter)
for num in list_iter:
if num < minimum:
minimum = num
return minimum
In response to Mathias' comment, here is a version that works with an iterable:
def my_min(seq):
seq = iter(seq)
try:
minimum = next(seq)
for num in seq:
if num < minimum:
minimum = num
return minimum
except StopIteration as e:
pass
raise ValueError('Empty list')
$endgroup$
iterators
You can use an iterator
def my_min(num_list):
# empty lists
if not num_list:
raise ValueError('Empty list')
list_iter = iter(num_list)
minimum = next(list_iter)
for num in list_iter:
if num < minimum:
minimum = num
return minimum
In response to Mathias' comment, here is a version that works with an iterable:
def my_min(seq):
seq = iter(seq)
try:
minimum = next(seq)
for num in seq:
if num < minimum:
minimum = num
return minimum
except StopIteration as e:
pass
raise ValueError('Empty list')
edited 5 hours ago
answered 8 hours ago
RootTwoRootTwo
1,2293 silver badges6 bronze badges
1,2293 silver badges6 bronze badges
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around thenext
call, you could handle all kind of iterables, not only lists.
$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
add a comment |
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around thenext
call, you could handle all kind of iterables, not only lists.
$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
1
1
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around the
next
call, you could handle all kind of iterables, not only lists.$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
This is the right approach to avoid duplicating the whole list in memory. If only you took the special case of the empty parameter around the
next
call, you could handle all kind of iterables, not only lists.$endgroup$
– Mathias Ettinger
6 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
$begingroup$
@MathiasEttinger good idea. Added it to the answer.
$endgroup$
– RootTwo
5 hours ago
add a comment |
$begingroup$
Review
I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
- You have implemented a simple function, so it shouldn't have been that hard to provide a couple of unit tests. You would have immediately found bugs on the most obvious edge cases as (1) empty list and self-created edge case (2) single item.
While insignificant in terms of wasted time and resources, I just
find it lacking elegance.
- What you gain in elegance is lost by the edge case guards you'd have to build in to fix the bugs.
$endgroup$
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
$begingroup$
Review
I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
- You have implemented a simple function, so it shouldn't have been that hard to provide a couple of unit tests. You would have immediately found bugs on the most obvious edge cases as (1) empty list and self-created edge case (2) single item.
While insignificant in terms of wasted time and resources, I just
find it lacking elegance.
- What you gain in elegance is lost by the edge case guards you'd have to build in to fix the bugs.
$endgroup$
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
$begingroup$
Review
I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
- You have implemented a simple function, so it shouldn't have been that hard to provide a couple of unit tests. You would have immediately found bugs on the most obvious edge cases as (1) empty list and self-created edge case (2) single item.
While insignificant in terms of wasted time and resources, I just
find it lacking elegance.
- What you gain in elegance is lost by the edge case guards you'd have to build in to fix the bugs.
$endgroup$
Review
I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
- You have implemented a simple function, so it shouldn't have been that hard to provide a couple of unit tests. You would have immediately found bugs on the most obvious edge cases as (1) empty list and self-created edge case (2) single item.
While insignificant in terms of wasted time and resources, I just
find it lacking elegance.
- What you gain in elegance is lost by the edge case guards you'd have to build in to fix the bugs.
answered 8 hours ago


dfhwzedfhwze
8,4411 gold badge18 silver badges49 bronze badges
8,4411 gold badge18 silver badges49 bronze badges
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
To your second point, are you saying there's no way to get the best of both worlds and therefore comparing the first element to itself is the way to go in terms of readability of code, etc...?
$endgroup$
– jeremy radcliff
8 hours ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226060%2freimplementation-of-min-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
what happens if num_list has only 1 element?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
@dfhwze, I should check for the list having no elements anyway, so I could check for its having only 1 element and return that element
$endgroup$
– jeremy radcliff
8 hours ago
$begingroup$
If the purpose is to comply to the specification of min, you are far away from home: programiz.com/python-programming/methods/built-in/min.
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
@dfhwze, good point, I edited my question
$endgroup$
– jeremy radcliff
8 hours ago