There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!

Agree with a comment or answer? Like () to up-vote the contribution!

Hello!
I am wondering why this code does not work:

def find_min(my_list):
min = None
if len(my_list) == 0:
return min
while len(my_list) > 0:
element = my_list[0]
print(my_list)
print(element)
if min is None:
min = element
elif element < min:
min = element
my_list.remove(element)
find_min(my_list)
return min

When calling print(find_min([4, 9, 2]))
it returns 4 and prints:

[4, 9, 2]
4
[9, 2]
9
[2]
2
4

Somehow, it fails to replace 4 with 2 as the min value.

Define find_min() so that it uses RECURSIVE calls. Do not use iteration!

#Implement your version of find_min() which has the same functionality using recursive calls!
def find_min(l):
if not l:
return None
else:
return l[0] if l[0] == min(l) else find_min(l[1:])
# test cases
print(find_min([42, 17, 2, -1, 67]) == -1)
print(find_min([]) == None)
print(find_min([13, 72, 19, 5, 86]) == 5)
print(find_min([1]) == 1)

To clarify, the error message is accurate, it’s triggered by that your function immediately returns the result without recursing for the rest when the first value is the smallest - it’s cheating by looking at the answer as reported by min.

it takes many times more time than the built-in. slower is expected, but not by such a large factor. this gets worse with larger input, but you’ll hit the recursion depth limit before that

it’ll reach maximum recursion depth very quickly, so it’ll only operate on small lists, it’s possible to get a max depth of log N which would make the depth fairly low even for very large input

So what you’d want to do instead is to say that the minimum of the overall thing is the smallest of the current value and the minimum of the rest.

To avoid copying, you can use indices instead of a list. So you’d specify the start location.

But you’ll also recurse as deep as the list is long, and you only get about 1000 depth. To solve that, you can recurse on each half. By splitting the input in half, the depth becomes half as much too, but you do it twice so you end up with the overall same thing. And it’s not just half, it’s half at each level. How many times do you have to halve a list to get it to size 1? log N times. log N will therefore be the max depth.

It’s also worth noting that minimum is a specialization of reduce.
There’s an initial value, you combine each value into the accumulated result by keeping the smaller one. Reduce can do the same thing with splitting the input in half, the overall order is still left to right provided that the left half is done first and then the right half. (in the case of min though, the order does not matter)

You might want to use two functions, one for accepting input and checking for empty, and another that has a different signature involving indices instead of lists.

Thank you for trying to explain all this. I just moved on. The course was aggravating me an maybe in the future I’ll revisit it after I forget about this.

The iterative definition of this problem in Codecademy has a bug
If you pass a list that contains the element 0, it totally messes up the function,
and the wrong minimum is returned. For example,

find_min([13, 72, 19, 5, 0, 86])

returns 86 as the minimum element, instead of 0

The method definition should be:-

def find_min(my_list):
min = 0
for element in my_list:
if element < min:
min = element
return min

Hi all having an issue in this exercise I need some help with:

This is my code:

def find_min(my_list):
if len(my_list) == 0:
return None
if len(my_list) == 1:
result = my_list[0]
print("result is " + str(result))
for i in my_list:
return i
if my_list[-2] < my_list [-1]:
print("removing " + str(my_list.pop()))
find_min(my_list)
#print(my_list)
else:
my_list.pop(-2)
find_min(my_list)
#print(my_list)
print(find_min([1, 2, 3]))

When I run the code I expected to get 1 as the result of the call of:

print(find_min([1, 2, 3]))

Instead I get as the printout:

removing 3
removing 2
result is 1
None

as you can see I get None as the return value not 1.