How to find the second lowest value of an array in python without using built-in functions?

1

I'm new to programming and I'm having trouble creating a code that returns the second value of an array , eg

array = [2, 3, 6, 6, 5]

return 3

I'm in that phase of practicing programming logic, because I already understand the whole basic set of logic, but since I have little practice in applying, I still run into problems that I believe are simple, anyway, I hope you understand.     

asked by anonymous 19.10.2017 / 19:56

2 answers

1
def segMenor(numeros):
     m1, m2 = float('inf'), float('inf')
     for x in numeros:
         if x <= m1:
             m1, m2 = x, m1
         elif x < m2:
             m2 = x
     return m2

 print segMenor([2, 3, 6, 6, 5])
    
19.10.2017 / 19:58
0

You can first use SET to organize the data, ensuring sorting and no duplicate copies .

    array = [2, 3, 6, 6, 5]
    copia = set(array)

The content of the copy variable is "[2,3,5,6]", ie we guarantee that the second value is always the second smaller.

After this you can print the desired value as follows:

   print (list(copia)[1]) 
    
23.10.2017 / 16:04