Society of Robots - Robot Forum

Software => Software => Topic started by: SciTech02 on October 17, 2008, 01:31:22 PM

Title: How to pick an item on a list with the lowest value?
Post by: SciTech02 on October 17, 2008, 01:31:22 PM
This may be a dumb question, but I am stumped on this.  The programing language is Python and I have a list of ordered pairs like this:

listX = [[1, 2], [3, 4], [2, 2], [0, 0]]

...and an adding function like this:

add(x, y): return x + y

Now what I'm trying to do is choose an ordered pair from the list with the lowest sum.  I created a for... loop to do this, but I only get a meaningful answer if I compare the function's value to an integer.  Example:

for r in listX:
   if add(r) < {given integer}:
     print r
   else:
     print "This pair is not less than {given integer}"

What could work is instead of comparing "add(r)" to an integer, is to compare it to the values of the rest of the list.  Pseudocode example:

if add(r) < {add(r) of the other values in listX}:

However, I do not know how to implement this.  This method could be entirely wrong as well, I don't know. 

All input will be appreciated.  :)
Title: Re: How to pick an item on a list with the lowest value?
Post by: pomprocker on October 17, 2008, 02:03:20 PM
Use a sorting algorithm, then you can pick your item from the same place every time.
Title: Re: How to pick an item on a list with the lowest value?
Post by: bens on October 17, 2008, 02:24:31 PM
Your should iterate through your list and compare the current value to the smallest value previously encountered.  If the current value is smaller, store it as the new smallest value and go on.  When you're done, the value you're storing will be the smallest in the entire list.  For example in C-like pseudo-code:

list = [ 5, 7, 2, ..., 9]

// the smallest element we know about so far is just the first element:
int smallest = list[0]  // value of the smallest array element found so far
int smallest_idx = 0  // array index of smallest element

for i = 1 to list length - 1  // iterate through the array
{
  if (list[ i ] < smallest)  // if we've found something even smaller
  {
    smallest = list[ i ];  // update what we're considering to be the smallest value
    smallest_idx = i;  // and store it's array index so we can find it later if we want to
  }
}

// here smallest is the smallest element in the array list
// and smallest_idx is the array index of this smallest element in list

- Ben
Title: Re: How to pick an item on a list with the lowest value?
Post by: SciTech02 on October 17, 2008, 05:35:01 PM
Great, you guys answered my question.  I can now use my program.  :) ;D It's strange I couldn't find any documentation about trying to do this, seems like it should be frequently used (maybe I didn't look hard enough?).

Anyway, thanks.  8)
Title: Re: How to pick an item on a list with the lowest value?
Post by: bens on October 20, 2008, 04:17:14 PM
If you look up sorting techniques this will be one of the first things you see.  It's one step of what is known as a "selection sort", where you run through a list looking for the smallest (or largest value) and swap it with the first element.  You then repeat the process by searching for the next smallest (or largest) element (which you do by looking everything in the list except the first element) and swapping what you find with the second element.  You continue this approach until you've sorted the entire list.

- Ben