-5

I feel confused about the code as follows:

define this findmin function to find the smallest number in alist, O(n^2)

def findMin(alist):
    overallmin=alist[0] 
    for i in alist:
        issmallest=True
        for j in alist:
            if i>j:
               issmallest=False
        if issmallest:
            overallmin = i
    return overallmin

I can't understand: why the author set "issmallest = True" below the first for loop? Commonly when we feel like to use boolean values like this situation(such as assign boolean value to a variable at the beginning of the code or put the boolean values as the if-statement conditions)? Thanks!

Community
  • 1
  • 1
Elena
  • 11
  • 4
  • That code is bad and doesn't work, I'd recommend learning from better examples. Also it doesn't contain `while True:`, although trivial research would find you e.g. https://stackoverflow.com/questions/3754620/a-basic-question-about-while-true – jonrsharpe Jan 06 '18 at 09:55
  • Your question doesnt make a lot of sense. You should consider simplifying it, you're asking a lot of questions and possibly confusing yourself. As for 1. Why the author set issmallest=True.. that would mean an assumption is made that the value is always the smallest, unless `i>j` – Alan Kavanagh Jan 06 '18 at 09:56

2 Answers2

1

Okay so first off, to erase a few of the mistakes in the code, a working example would be:

def findMin(alist):
    overallmin=alist[0] 

    for i in alist:
        issmallest=True
        for j in alist:
            if i>j:
               issmallest=False
        if issmallest:
            overallmin = i
    return overallmin

The idea behind this code is to compare all elements in a list with all others and keep the one that is smaller than all others. In the loop it is therefore assumed that the current element is the smallest issmallest = True until a smaller one is found. If a smaller one is found, the value of issmallest is changed to False. So if, after comparison with all others, issmallest is still True, then the element is truly the smallest and therefore fixed as such.

You could simplify this code , as there is no need in further comparing once the smallest element is found, i.e. you can leave the function. Also in this algorithm there is no need of keeping a variable for the smallest element. The corresponding code might read something like:

def findMin(alist): 
    for current_el in alist:
        issmallest = True
        for other_el in alist:
            if other_el < current_el:
               issmallest = False

        if issmallest:
            return current_el

But: Even for beginners, this is not a good code to find the minimum. I can say that as a beginner myself. It is much cleaner to go through the list once, with an element at hand, compare it while going through, and always keep the smallest. So even with low afford you can write a much faster algorithm like this:

def findMin(alist): 
    smallest_el = alist.pop() # take out an element, no need to compare with itself
    for other_el in alist:
        if other_el < smallest_el:
            smallest_el = other_el

    return smallest_el
Banana
  • 1,149
  • 7
  • 24
0

The condition of while always must evaluate to boolean. if it evaluates to true, the loop continues, otherwise, the statements after the loop will be executed. Please note: the code in your question did not involve a while loop. This code is very bad, please forget it, and everything that came with it. For learning, try this: https://www.learnpython.org/.

Peter Szabo
  • 1,056
  • 2
  • 14
  • 30