title: Greedy algorithm in Python
body: <p>I want c in plunder(aList, c) to equal 0. </p>
  
  <pre><code>List = [('Gold', 10, 500), ('Silver', 5, 200), ('Diamond', 2, 2000), ('Platinum', 20, 1000)]
  aList = sorted(List, key =  lambda x : x[2]) # sort the list above
  </code></pre>
  
  <p>Gives me the sorted list based on the 3rd value of each tuple. So I get:</p>
  
  <pre><code>[('Silver', 5, 200), ('Gold', 10, 500), ('Platinum', 20, 1000), ('Diamond', 2, 2000)]
  </code></pre>
  
  <p>I am trying to get the plunder(aList, c) to keep subtracting the middle values of each tuple (2, 20, 10, 5) from c until c = 0. </p>
  
  <p>Here is my code: </p>
  
  <pre><code>List = [('Gold', 10, 500), ('Silver', 5, 200), ('Diamond', 2, 2000), ('Platinum', 20, 1000)]
  aList = sorted(List, key =  lambda x : x[2]) # sort the list above
  
  def plunder(aList, c):
      aList[-1] = list(aList[-1])
      i = aList[-1]
      r = 0
      if c &gt; 0 and i[1] != 0:
          c -= 1
          i[1] -=1
          r += 1
          return plunder(aList, c-r)
      elif c == 0:
          pass
          print('Done')
      else:
          return plunder(aList[:-1], c-r)
  
  plunder(aList, 10)
  </code></pre>
  
  <p>But when I run it, it prints done and the new list is:</p>
  
  <pre><code>[('Silver', 5, 200), ('Gold', 10, 500), ('Platinum', 20, 1000), ['Diamond', 0, 2000]]
  </code></pre>
  
  <p>and also when I type c in the python shell, it tells me that c is not defined. How could I fix these issues?</p>
  
  <p>So if the c value is 10. My expected output would be:</p>
  
  <pre><code>[('Silver', 5, 200), ('Gold', 10, 500), ['Platinum', 12, 1000], ['Diamond', 0, 2000]]
  </code></pre>
  
  <p>I subtracted as many diamonds as I could from the 10 (10 - 2 = 8) so 0 diamonds were left. Then I subtracted 8 from 20 platinums and the weight of the platinums changed to 12 (since I took 8 platinums. And now my c ('capacity') is 0. 2 diamonds + 8 platinums = 10 (which was my c).</p>