CabooseMSG - 1 year ago 45

Python Question

So I'm tasked with finding the third lowest value of a given list. My code looks like

`def findMinIndex(L, startIndex):`

minIndex = startIndex

currIndex = minIndex + 1

while currIndex < len(L):

if L[currIndex] < L[minIndex]:

minIndex = currIndex

currIndex = currIndex + 1

return minIndex

def thirdSmallest(L):

i = 0

while i < len(L):

minIndex = findMinIndex(L, i)

L[i], L[minIndex] = L[minIndex], L[i]

i = i + 1

print(L[2])

thirdSmallest([1, 99, 7, -3, 3, 10, 12])

The list I have for L should print 3 as the 3rd lowest value, but anaconda is taking an incredibly long time to return anything to me. I was given a hint that I should modify

`while i < len(L):`

or my print function. But I dont see what I should do. Any advice?

Answer

I don't understand what you want to do, but I've find one error, leading to potential infinite loop:

```
def findMinIndex(L, startIndex):
minIndex = startIndex
currIndex = minIndex + 1
while currIndex < len(L):
if L[currIndex] < L[minIndex]:
minIndex = currIndex
currIndex = currIndex + 1 # Error corrected here
return minIndex
```

Source (Stackoverflow)