Picking the smallest value in a list using recursion
up vote
1
down vote
favorite
This is a function I defined in order to find the smallest value in a list using recursion.However,I called the function twice within itself, which I think it's a bit weird.
Is there a way around the function append()
?. We haven't studied it yet so I'm asking whether there could be a simpler way to get the same solution by not using append()
?
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
if lst[0] < lst[1]:
lst.append(lst[0])
return(minimum(lst[1:]))
return(minimum(lst[1:]))
python list recursion
add a comment |
up vote
1
down vote
favorite
This is a function I defined in order to find the smallest value in a list using recursion.However,I called the function twice within itself, which I think it's a bit weird.
Is there a way around the function append()
?. We haven't studied it yet so I'm asking whether there could be a simpler way to get the same solution by not using append()
?
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
if lst[0] < lst[1]:
lst.append(lst[0])
return(minimum(lst[1:]))
return(minimum(lst[1:]))
python list recursion
It is not proper at all for a function likeminimum
to alter its argument...
– Antti Haapala
Nov 10 at 21:59
Just do theappend
and don't do thereturn
– Peter Wood
Nov 10 at 21:59
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This is a function I defined in order to find the smallest value in a list using recursion.However,I called the function twice within itself, which I think it's a bit weird.
Is there a way around the function append()
?. We haven't studied it yet so I'm asking whether there could be a simpler way to get the same solution by not using append()
?
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
if lst[0] < lst[1]:
lst.append(lst[0])
return(minimum(lst[1:]))
return(minimum(lst[1:]))
python list recursion
This is a function I defined in order to find the smallest value in a list using recursion.However,I called the function twice within itself, which I think it's a bit weird.
Is there a way around the function append()
?. We haven't studied it yet so I'm asking whether there could be a simpler way to get the same solution by not using append()
?
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
if lst[0] < lst[1]:
lst.append(lst[0])
return(minimum(lst[1:]))
return(minimum(lst[1:]))
python list recursion
python list recursion
edited Nov 10 at 21:58
asked Nov 10 at 21:52
user10158754
It is not proper at all for a function likeminimum
to alter its argument...
– Antti Haapala
Nov 10 at 21:59
Just do theappend
and don't do thereturn
– Peter Wood
Nov 10 at 21:59
add a comment |
It is not proper at all for a function likeminimum
to alter its argument...
– Antti Haapala
Nov 10 at 21:59
Just do theappend
and don't do thereturn
– Peter Wood
Nov 10 at 21:59
It is not proper at all for a function like
minimum
to alter its argument...– Antti Haapala
Nov 10 at 21:59
It is not proper at all for a function like
minimum
to alter its argument...– Antti Haapala
Nov 10 at 21:59
Just do the
append
and don't do the return
– Peter Wood
Nov 10 at 21:59
Just do the
append
and don't do the return
– Peter Wood
Nov 10 at 21:59
add a comment |
5 Answers
5
active
oldest
votes
up vote
3
down vote
accepted
I guess you could do this to avoid append:
def minimum(lst):
if len(lst)==1:
return lst[0]
if lst[0] < lst[1]:
return minimum(lst[0:1]+ lst[2:])
else:
return minimum(lst[1:])
but i think this one is better with only one call to minimum:
def minimum(lst):
if len(lst) == 1:
return lst[0]
s = minimum(lst[1:])
return s if s < lst[0] else lst[0]
return minimum(lst[0:1]+ lst[2:])
isn'tlst[0:1]
andlst[0]
one and the same ?
– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
add a comment |
up vote
1
down vote
Use an additional variable?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)
add a comment |
up vote
1
down vote
Here's a very explicit version that should be easy to read due to comments and variable names.
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
add a comment |
up vote
-1
down vote
You can use the following program:
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
temp_min = minimum(lst[1:])
if lst[0] < temp_min:
return lst[0]
else:
return temp_min
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
add a comment |
up vote
-1
down vote
If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:
def smallest(list):
return min(list)
list_of_ints = [4,2,7,6,8,1,5,9,2]
print(smallest(list_of_ints))
This will print out 1.
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
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: "1"
;
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53243766%2fpicking-the-smallest-value-in-a-list-using-recursion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I guess you could do this to avoid append:
def minimum(lst):
if len(lst)==1:
return lst[0]
if lst[0] < lst[1]:
return minimum(lst[0:1]+ lst[2:])
else:
return minimum(lst[1:])
but i think this one is better with only one call to minimum:
def minimum(lst):
if len(lst) == 1:
return lst[0]
s = minimum(lst[1:])
return s if s < lst[0] else lst[0]
return minimum(lst[0:1]+ lst[2:])
isn'tlst[0:1]
andlst[0]
one and the same ?
– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
add a comment |
up vote
3
down vote
accepted
I guess you could do this to avoid append:
def minimum(lst):
if len(lst)==1:
return lst[0]
if lst[0] < lst[1]:
return minimum(lst[0:1]+ lst[2:])
else:
return minimum(lst[1:])
but i think this one is better with only one call to minimum:
def minimum(lst):
if len(lst) == 1:
return lst[0]
s = minimum(lst[1:])
return s if s < lst[0] else lst[0]
return minimum(lst[0:1]+ lst[2:])
isn'tlst[0:1]
andlst[0]
one and the same ?
– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I guess you could do this to avoid append:
def minimum(lst):
if len(lst)==1:
return lst[0]
if lst[0] < lst[1]:
return minimum(lst[0:1]+ lst[2:])
else:
return minimum(lst[1:])
but i think this one is better with only one call to minimum:
def minimum(lst):
if len(lst) == 1:
return lst[0]
s = minimum(lst[1:])
return s if s < lst[0] else lst[0]
I guess you could do this to avoid append:
def minimum(lst):
if len(lst)==1:
return lst[0]
if lst[0] < lst[1]:
return minimum(lst[0:1]+ lst[2:])
else:
return minimum(lst[1:])
but i think this one is better with only one call to minimum:
def minimum(lst):
if len(lst) == 1:
return lst[0]
s = minimum(lst[1:])
return s if s < lst[0] else lst[0]
edited Nov 10 at 22:07
coldspeed
115k18105182
115k18105182
answered Nov 10 at 21:55
Christian Sloper
1,058213
1,058213
return minimum(lst[0:1]+ lst[2:])
isn'tlst[0:1]
andlst[0]
one and the same ?
– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
add a comment |
return minimum(lst[0:1]+ lst[2:])
isn'tlst[0:1]
andlst[0]
one and the same ?
– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
return minimum(lst[0:1]+ lst[2:])
isn't lst[0:1]
and lst[0]
one and the same ?– user10158754
Nov 10 at 22:13
return minimum(lst[0:1]+ lst[2:])
isn't lst[0:1]
and lst[0]
one and the same ?– user10158754
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
no, they are not
– Christian Sloper
Nov 10 at 22:13
1
1
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
one is a list of one integer ex: [1], the other an integer ex: 1
– Christian Sloper
Nov 10 at 22:14
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
important here as [1] + 2 is not allowed, while [1] + [2] gives [1,2]
– Christian Sloper
Nov 10 at 22:15
add a comment |
up vote
1
down vote
Use an additional variable?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)
add a comment |
up vote
1
down vote
Use an additional variable?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)
add a comment |
up vote
1
down vote
up vote
1
down vote
Use an additional variable?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)
Use an additional variable?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)
answered Nov 10 at 22:02
roeen30
43629
43629
add a comment |
add a comment |
up vote
1
down vote
Here's a very explicit version that should be easy to read due to comments and variable names.
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
add a comment |
up vote
1
down vote
Here's a very explicit version that should be easy to read due to comments and variable names.
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
add a comment |
up vote
1
down vote
up vote
1
down vote
Here's a very explicit version that should be easy to read due to comments and variable names.
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
Here's a very explicit version that should be easy to read due to comments and variable names.
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
answered Nov 10 at 22:05
timgeb
48.1k116390
48.1k116390
add a comment |
add a comment |
up vote
-1
down vote
You can use the following program:
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
temp_min = minimum(lst[1:])
if lst[0] < temp_min:
return lst[0]
else:
return temp_min
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
add a comment |
up vote
-1
down vote
You can use the following program:
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
temp_min = minimum(lst[1:])
if lst[0] < temp_min:
return lst[0]
else:
return temp_min
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
add a comment |
up vote
-1
down vote
up vote
-1
down vote
You can use the following program:
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
temp_min = minimum(lst[1:])
if lst[0] < temp_min:
return lst[0]
else:
return temp_min
You can use the following program:
def minimum(lst):
"""
parameters : lst of type list
return : the value of the smallest element in the lst
"""
if len(lst) == 1:
return lst[0]
temp_min = minimum(lst[1:])
if lst[0] < temp_min:
return lst[0]
else:
return temp_min
edited Nov 10 at 22:12
answered Nov 10 at 21:59
ask
727
727
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
add a comment |
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
I struggled if it was ok to use "min" or not :-) return min(lst) is awfully tempting then :-) (not me that downvoted)
– Christian Sloper
Nov 10 at 22:01
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
@ChristianSloper You are right sir. I missed that, I should have used if condition. And yes, Sorry I did not see that you have answered correct while I was writing my answer. :-(
– ask
Nov 10 at 22:03
add a comment |
up vote
-1
down vote
If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:
def smallest(list):
return min(list)
list_of_ints = [4,2,7,6,8,1,5,9,2]
print(smallest(list_of_ints))
This will print out 1.
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
add a comment |
up vote
-1
down vote
If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:
def smallest(list):
return min(list)
list_of_ints = [4,2,7,6,8,1,5,9,2]
print(smallest(list_of_ints))
This will print out 1.
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
add a comment |
up vote
-1
down vote
up vote
-1
down vote
If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:
def smallest(list):
return min(list)
list_of_ints = [4,2,7,6,8,1,5,9,2]
print(smallest(list_of_ints))
This will print out 1.
If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:
def smallest(list):
return min(list)
list_of_ints = [4,2,7,6,8,1,5,9,2]
print(smallest(list_of_ints))
This will print out 1.
edited Nov 10 at 22:37
answered Nov 10 at 22:10
john doe
14
14
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
add a comment |
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
the min function is better than sorting? so if you want to use built in functions go with min(list)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
sorting is O(n log n) , min function is O(n)
– Christian Sloper
Nov 10 at 22:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53243766%2fpicking-the-smallest-value-in-a-list-using-recursion%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
It is not proper at all for a function like
minimum
to alter its argument...– Antti Haapala
Nov 10 at 21:59
Just do the
append
and don't do thereturn
– Peter Wood
Nov 10 at 21:59