I am new to Python and learning by myself. This is my first post here, I appreciate any help that you can give me.
I have been trying to find the min value and the index on a list with a function.
This is the code that I wrote:
def findMin (L,startIndx):
m = L[startIndx]
index = startIndx
for i in range (startIndx,len(L)):
x = L[i]
if i < m:
i = index
m = x
else:
pass
return (m,index)
a,b = findMin([8,2,11,0,5])
print (a,b)
This is the error that I get:
**TypeError Traceback (most recent call last)
<ipython-input-33-9713029875a6> in <module>
----> 1 a,b = findMin([8,2,11,0,5])
2 print (a,b)
TypeError: findMin() missing 1 required positional argument: 'startIndx'**
I truly have no idea what is the problem with it, I appreciate any help,
CodePudding user response:
The problem that you ran into is that you only passed one argument (L) and not the second one (startIndx) and you have some logic errors in your code as mentioned by other answers.
This is a much shorter way of writing your function using some built-in functions that isn't that error-prone.
Very compact form:
def findMin(L,startIndx=0):
m = min(L[startIndx:])
return (m, L.index(m))
Here is the more spaced out version of that function with some explenations.
#Notice that startIndx is a OPTIONAL ARGUMENT. This means if we don't pass anything to it, it will be defined to be 0
def findMin(L,startIndx=0):
#The searchspace is defined by SLICING the list using list[from:upto]
searchspace = L[startIndx:]
#Using the built-in min() function we find the smallest value in the list
m = min(searchspace)
#Then we use the built-in list.index(value) function to find the index of the smallest element
#Quick reminder: The first value in a list has the index 0
index = L.index(m)
#Finally we return the needed values
return (m, index)
This is how the function is called with startIndx:
test_list = [5,0,9,4,11]
test_index = 3
a,b = findMin(test_list, test_index)
print(a,b)
#--> 4, 3
When you call the function without the startIndx argument it searches through the entire list, because the optional argument startIndx is set to 0.
test_list = [5,0,9,4,11]
a,b = finMin(test_list)
print(a,b)
#--> 0, 2
CodePudding user response:
Call with two parameters; findMin([8,2,11,0,5])
passes a single list. Perhaps findMin([8,2,11,0,5], 0)
.
Indentation appears to be incorrect, and this is critical in Python. All lines from "m =" to "return" must be indented one more level.
Variable naming could be improved; part of the confusion is likely from names like "m", "x", etc. What does m
mean, vs. for example max_val
?
Indexing seems confused; first you set index = startIndx
then use for i
. Delete the index =
and use for index in range (startIndx 1, len(L))
(you already have L[startIndx]).
What is the desired behavior and return value if startIndx is > len(L)?
Why comparing i to m?
Optimization: don't need the else: pass
. It means "otherwise, do nothing" which does not need to be explicit.
CodePudding user response:
So I can see a few errors in your algorithm.
First error:
findMin() missing 1 required positional argument: 'startIndx'
This error is due to the fact that findMin is a function that takes two arguments. First argument is the array you want to find the minimum of ([8,2,11,0,5] in your example). The second one is the startIndex. It is the index which you want to start at while searching for the min. You can add this index to your function call or add a default value in your funtion declaration.
Second error:
line 6: if i < m:
This is not what you want to do. Here you are comparing i (which is the current index of your for loop) and m which is the minimum value so far. You want to compare x and m like this:
if x < m
Third and last error:
i = index
This is wrong. You want to swap these two variables and assign i to index like this:
index = i
Here is the final correct code:
def findMin (L,startIndx):
m = L[startIndx]
index = startIndx
for i in range (startIndx 1,len(L)):
x = L[i]
if x < m:
index = i
m = x
else:
pass
return (m, index)
a,b = findMin([8,2,11,3,1], 0)
print (a,b)
CodePudding user response:
findMin
has two parameters, but you only pass it one argument. You have two choices:
Pass the argument that
findMin
requires.a,b = findMin([8,2,11,0,5], 0)
Remove the
startIndx
parameter and use0
instead:
def findMin (L):
index = 0
m = L[index]
for i in range (len(L)):
x = L[i]
if x < m:
index = i
m = x
else:
pass
return (m,index)
CodePudding user response:
def findMin (L,startIndx):
this function will expect 2 arguments L
and startIndx
. So you'll have to pass 2 arguments as well. But you are passing only 1 argument findMin([8,2,11,0,5])
.
[8,2,11,0,5]
as a list is one argument passed to L
, then missing one for startIndx
which results in error.
startIndx
seems unnecessary as well since all the elements needs to be traverse.
def findMin(L):
m = L[0]
index = 0
for i in range (1,len(L)):
if L[i]<m:
m=L[i]
index=i
return (m,index)
a,b = findMin([8,2,11,0,5])
print (a,b)