0
dict1 = {datetime.timedelta(3, 21388, 702926): 2, datetime.timedelta(10, 21388, 702926): 1, datetime.timedelta(3, 65011, 297074): 2, datetime.timedelta(14, 65011, 297074): 1, datetime.timedelta(17, 65011, 297074): 1, datetime.timedelta(0, 65011, 297074): 1, datetime.timedelta(7, 65011, 297074): 1, datetime.timedelta(10, 65011, 297074): 1, datetime.timedelta(0, 21388, 702926): 1}

I am trying to remove the timedeltas, the sum of whose values equals 6. They also have to be the largest timedeltas in the dict.

Here's how I'm trying to solve it:

x = 0
for key in dict1:
    if key in sorted(dict1)[-1] and x < 6:
        x = x+dict1[key]
        del dict1[key]

My thinking is sorted(dict) returns a list of timedeltas with the largest ones at the end. I could match each key with the largest timedelta in the list, sum its value to x & remove that key until the x reaches 6. But this returns:

TypeError: argument of type 'datetime.timedelta' is not iterable

Stumped again.

koogee
  • 943
  • 3
  • 12
  • 24
  • 2
    Don't use `dict` as a name, you are masking the built-in type. – Martijn Pieters Feb 14 '14 at 13:42
  • Take a look at this question see if it helps you - http://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python – Dayan Feb 14 '14 at 13:42
  • What if the sum of the largest keys is *not* exactly 6? – Martijn Pieters Feb 14 '14 at 13:43
  • What if the sum two non-consecutive values is 6 ? e.g. largest + smallest = 6 – Sunny Nanda Feb 14 '14 at 13:44
  • @MartijnPieters good question, I was thinking of handling that later. 6 here is arbitrary. Pick 5. – koogee Feb 14 '14 at 13:44
  • @SunnyNanda has to be the largest. the largest ones whose values add up to 6 need to go. – koogee Feb 14 '14 at 13:46
  • For the record, `sorted(dict1)` returns a list of (sorted) keys, so your `timedelta` objects. `[-1]` is the last one, the largest. You then try to use a membership test on that with `key in`. That's not working here. But your approach has bigger problems than that. – Martijn Pieters Feb 14 '14 at 13:47
  • of course it does. Im not a programmer by trade :) – koogee Feb 14 '14 at 13:49
  • if `dict1` have `6` elements all with value of `1` would you expect the answer to be empty `dict`? – podshumok Feb 14 '14 at 13:49
  • 1
    @koogee: look up "knapsack problem", you have one here. – Martijn Pieters Feb 14 '14 at 13:49
  • You would like to find the keys which pair of values sums 6 ? – tk. Feb 14 '14 at 13:52
  • @podshumok In this particular case, dict1 will always have keys, the sum of whose values will be more than 6. Please use this as premise. – koogee Feb 14 '14 at 13:52
  • @tk not just keys whose values sum to 6, but the _largest_ keys whose values sum to 6. – koogee Feb 14 '14 at 13:53
  • @koogee ok, is there a limit on number of deletions from `dict1`? – podshumok Feb 14 '14 at 13:55
  • 1
    @koogee but can you have more than 2 values in the sum ? – tk. Feb 14 '14 at 13:55
  • @podshumok how do you mean? the limit is the sum total 6. When sum of values of the largest keys approaches 6, the deletions should end. – koogee Feb 14 '14 at 13:57
  • @tk didnt quite understand that...? it doesnt matter if three values make up 6 or six values make up 6. Their corresponding keys must be the largest in the timedeltas. – koogee Feb 14 '14 at 14:00
  • @koogee suppose you have `3` big keys with values of `2` and `2` small keys with values of `3`. What you expect to be deleted? – podshumok Feb 14 '14 at 14:05
  • @podshumok 3 big keys with values of 2. The keys have to be the largest. – koogee Feb 14 '14 at 14:07
  • @kogee and if you have one more, the biggest key with value of `1`? – podshumok Feb 14 '14 at 14:14
  • @podshumok I get the problem you're pointing at. The sum of the largest keys may not always equal 6. I'm going to have to think of something to by pass that issue. But I think the gist of the original problem was figuring how to remove the largest keys based on sum of values. – koogee Feb 14 '14 at 14:21
  • @podshumok I should have set the sum to 4 for the purposes of this question. – koogee Feb 14 '14 at 14:22

1 Answers1

1

From the comments I deduce that you want to delete the biggest timedeltas until the aggregated sum of their values reaches 6.

values_sum = 0
for key in sorted(dict1,reverse=True):
    values_sum +=dict1[key]
    del dict1[key]
    if values_sum >= 6:
        break    
tk.
  • 626
  • 4
  • 14
  • Thank you. It worked well for the problem presented. I'm going to test it a bit more and also look at the scenario when sum of the largest keys is not _exactly_ 6 like in this case. – koogee Feb 14 '14 at 14:17