views:

198

answers:

2

Hello,

I've a bit of a problem. I'm adding numbers to ArrayList like 156, 340 (when it is TransferIn or Buy) etc and then i remove them doing it like 156, 340 (when it's TransferOut, Sell). Following solution works for that without a problem. The problem I have is that for some old data employees were entering sum's like 1500 instead of 500+400+100+500. How would I change it so that when there's Sell/TransferOut and there's no match inside ArrayList it should try to add multiple items from that ArrayList and find elements that combine into aggregate.

   ArrayList alNew = new ArrayList();
   ArrayList alNewPoIle = new ArrayList();
   ArrayList alNewCo = new ArrayList();
   string tempAkcjeCzynnosc = (string) alInstrumentCzynnoscBezNumerow[i];
   string tempAkcjeInId = (string) alInstrumentNazwaBezNumerow[i];
   decimal varAkcjeCena = (decimal) alInstrumentCenaBezNumerow[i];
   decimal varAkcjeIlosc = (decimal) alInstrumentIloscBezNumerow[i];
   int index;
   switch (tempAkcjeCzynnosc) {                  

          case "Sell":
          case "TransferOut":
          index = alNew.IndexOf(varAkcjeIlosc);
          if (index != -1) {
              alNew.RemoveAt(index);
              alNewPoIle.RemoveAt(index);
              alNewCo.RemoveAt(index);
          } else {
              // Number without match encountred
          }
          break;

          case "Buy":
          case "TransferIn":
               alNew.Add(varAkcjeIlosc);
               alNewPoIle.Add(varAkcjeCena);
               alNewCo.Add(tempAkcjeInId);
               break;
    }
}
+4  A: 

This could prove trickier than you might think:

LukeH
Well I have to somehow solve this as this added up in database. I could do it SUM and do CASE WHEN in there but problem it doesn't tell me which packets are kicked out and which are still in. We will be cleaning this mess when database goes live but until then I have to have working solution so that "results" are the one i expect it too. Later on i will delete 1500 and insert 5 Sell/TransferOut packets to resolve this. But this will be hand job so gonna take a while.
MadBoy
The list will vary. sometimes 30 items, sometimes less. sometimes more. Depending on how long we have him as customer.
MadBoy
Haven't seen more then 100.
MadBoy
Well I hope this will be temporary solution as I will be pushing employees to actually remove sums and put proper values in it's place, but until they do that i have to get counting right.
MadBoy
DP will work very fast for a few hundred elements. Assuming the numbers aren't huge anyway (in the ten thousands should be fine).
IVlad
Take a look at IVlad's answer, and the links in that answer. That's probably your best bet in this case.
LukeH
+3  A: 

This is a variation of the knapsack problem called the subset sum problem. Check my answer here for multiple solutions. To get the actual items you need to remove if you use the dynamic programming approach, just keep a second array that tells you what was the last element you added to get a certain sum, then you can use that to find the solution. Post back if you can't get it working. If you have a lot of numbers, I suggest the randomized algorithm anyway, it is both easier to implement and more memory and time-efficient (usually).

IVlad
Thanks, will try to work with it. Just had a talk with Director of department and he said they will those transactions before it will go live so hopefully there's no need for implementing this workaround.
MadBoy
IVlad if you're still so kind and can help me with subset sum problem please do. I've opened new question: http://stackoverflow.com/questions/2708436/subset-sum-problem
MadBoy