Note that there are some explanatory texts on larger screens.

plurals
  1. POSubset sum problem
    text
    copied!<p>I'm having a problem with counting which is continuation of <a href="https://stackoverflow.com/questions/2476400/counting-problem-c">this</a> question. I am not really a math person so it's really hard for me to figure out this <code>subset sum problem</code> which was suggested as resolution.</p> <p>I'm having 4 <code>ArrayList</code> in which I hold data: alId, alTransaction, alNumber, alPrice </p> <blockquote> <p>Type | Transaction | Number | Price<br> 8 | Buy | 95.00000000 | 305.00000000<br> 8 | Buy | 126.00000000 | 305.00000000<br> 8 | Buy | 93.00000000 | 306.00000000<br> 8 | Transfer out | 221.00000000 | 305.00000000<br> 8 | Transfer in | 221.00000000 | 305.00000000<br> 8 | Sell | 93.00000000 | 360.00000000<br> 8 | Sell | 95.00000000 | 360.00000000<br> 8 | Sell | 126.00000000 | 360.00000000<br> 8 | Buy | 276.00000000 | 380.00000000 </p> </blockquote> <p>In the end I'm trying to get what's left for customer and what's left I put into 3 array lists:<br> - alNewHowMuch (corresponds to alNumber),<br> - alNewPrice (corresponds to alPrice),<br> - alNewInID (corrseponds to alID)</p> <pre><code> ArrayList alNewHowMuch = new ArrayList(); ArrayList alNewPrice = new ArrayList(); ArrayList alNewInID = new ArrayList(); for (int i = 0; i &lt; alTransaction.Count; i++) { string transaction = (string) alTransaction[i]; string id = (string) alID[i]; decimal price = (decimal) alPrice[i]; decimal number = (decimal) alNumber[i]; switch (transaction) { case "Transfer out": case "Sell": int index = alNewHowMuch.IndexOf(number); if (index != -1) { alNewHowMuch.RemoveAt(index); alNewPrice.RemoveAt(index); alNewInID.RemoveAt(index); } else { ArrayList alTemp = new ArrayList(); decimal sum = 0; for (int j = 0; j &lt; alNewHowMuch.Count; j ++) { string tempid = (string) alNewInID[j]; decimal tempPrice = (decimal) alNewPrice[j]; decimal tempNumbers = (decimal) alNewHowMuch[j]; if (id == tempid &amp;&amp; tempPrice == price) { alTemp.Add(j); sum = sum + tempNumbers; } } if (sum == number) { for (int j = alTemp.Count - 1; j &gt;= 0; j --) { int tempIndex = (int) alTemp[j]; alNewHowMuch.RemoveAt(tempIndex); alNewPrice.RemoveAt(tempIndex); alNewInID.RemoveAt(tempIndex); } } } break; case "Transfer In": case "Buy": alNewHowMuch.Add(number); alNewPrice.Add(price); alNewInID.Add(id); break; } } </code></pre> <p>Basically I'm adding and removing things from Array depending on Transaction Type, Transaction ID and Numbers. 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). My 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 <code>Sell/TransferOut</code> or <code>Buy/Transfer In</code> and there's no match inside ArrayList it should try to add multiple items from that<code>ArrayList</code> and find elements that combine into aggregate. </p> <p>Inside my code I tried to resolve that problem with simple summing everything when there's no match (index == 1) </p> <pre><code> int index = alNewHowMuch.IndexOf(number); if (index != -1) { alNewHowMuch.RemoveAt(index); alNewPrice.RemoveAt(index); alNewInID.RemoveAt(index); } else { ArrayList alTemp = new ArrayList(); decimal sum = 0; for (int j = 0; j &lt; alNewHowMuch.Count; j ++) { string tempid = (string) alNewInID[j]; decimal tempPrice = (decimal) alNewPrice[j]; decimal tempNumbers = (decimal) alNewHowMuch[j]; if (id == tempid &amp;&amp; tempPrice == price) { alTemp.Add(j); sum = sum + tempNumbers; } } if (sum == number) { for (int j = alTemp.Count - 1; j &gt;= 0; j --) { int tempIndex = (int) alTemp[j]; alNewHowMuch.RemoveAt(tempIndex); alNewPrice.RemoveAt(tempIndex); alNewInID.RemoveAt(tempIndex); } } } </code></pre> <p>But it only works if certain conditions are met, and fails for the rest. </p> <p><strong>Edit:</strong> Since some of you were so astonished (and blinded) by my polish variable names i translated all of them to english for simplicity and visiblity. Hopefully this will help me to get some help :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload