1

I have seen many answered questions about this kind of topics, but I need to know what is the best way to limit the number of possible combinations given a list of values (1,5,10,20) and a limit number (100).

My question is, how can I do to avoid results like (100*1 or 20*5) and select only results with a limit of 10 units (4*20+4*5) or (3*20+7*5) or (9*10+1*5).

Unwanted combinations:

 (20*5), (100*1), (15*5+25*1), (40*1+12*5), etc

Desired combinations (equal or less than 100):

 (3*20+7*5), (8*10+1*20), (4*20+1*10+2*5), etc

What i want is all possible combinations under or equal 100 and, (supposing to be coins), combinations with no more than 10 coins.

This piece of code solves the problem of limiting results, but shows only one result:

   class Program
   {
   static int amount = 1000;

   static void Main(string[] args)
   {
        Coin[] c = new Coin[] { new Coin(500, 3), new Coin(200, 3), new Coin(100, 3) ,
                                new Coin(50, 3), new Coin(20, 3), new Coin(10, 3),
                                new Coin(5, 3), new Coin(2, 3), new Coin(1, 3)};
        int netAmount = amount;
        for (int i = 0; i < c.Length; i++)
        {
            amount -= c[i].coveredPrice(amount);
        }
        for (int i = 0; i < c.Length; i++)
        {
            Console.WriteLine(c[i].ToString());
        }
        Console.ReadLine();
    }
}

class Coin
{
    private int price;
    private int counted;    
    private int maxNo;

    public Coin(int coinPrice, int coinMaxNo)
    {
        this.price = coinPrice;
        this.maxNo = coinMaxNo;
        this.counted = 0;
    }

    public int coveredPrice(int Price)
    {
        int Num = Price / price;
        if (maxNo == 0)
            return 0;
        if (maxNo != -1)             
            if (Num > this.maxNo - this.counted)
                Num = maxNo;
        this.counted += Num;
        return Num * price;
    }
        public override string ToString()
    {
        return string.Format("{0} x {1} (max {2}) ", this.price.ToString(), this.counted.ToString(), this.maxNo.ToString());
    }
}

}

What should i have to modify to show me all the results?

Fernando
  • 23
  • 5
  • The question is unclear. – Ahmed Bahtity Aug 16 '18 at 09:43
  • Assuming your candidate numbers are stored in a list, you would pick a random number from 0 to the length of your list - 1. Instead of directly using that random number as your output, you should lookup that index if the candidate list, and return *that* value instead. That way your list defines the only valid output values. – Bradley Uffner Aug 16 '18 at 09:45
  • I´ll edit question to make it more clear. – Fernando Aug 16 '18 at 09:51
  • With combinations you do not need both 4 * 20 and 20 * 4. So with 1,5,10,20 You want 1:5,1:10,1:20,5:10,5:20,10:20. – jdweng Aug 16 '18 at 09:52
  • I´ve edited question. The sum of each combination must be equal or below 100. – Fernando Aug 16 '18 at 10:03
  • according to your question's examples of unwanted combinations you don't want equals... or do you? update the question along with the comments please – D Ie Aug 16 '18 at 10:26
  • What have you tried, where are you stuck? Also, please [read this](https://stackoverflow.com/help/how-to-ask)... – jeroenh Aug 16 '18 at 10:34
  • What exactly do you mean by 10 units? Also, why is `(20 * 5)` and `(100*1)` not wanted but `(8*10 + 1*20)` wanted? They both sum to 100 which meets your requirements. – Joseph Wood Aug 16 '18 at 14:20
  • What i want is all possible combinations under or equal 100 and, (supposing to be coins), combinations with no more than 10 coins. – Fernando Aug 16 '18 at 14:31
  • I have edited the question. – Fernando Aug 16 '18 at 14:33

1 Answers1

0

I did a quick example that I believe will help you.

string pattern = @"\(([^)]*)\)";
string expressions = "(4*20+4*5), (3*20+7*5), (9*10+1*5), (9*10+10*5)";
List<string> validCombinations = new List<string>();

Regex regex = new Regex(pattern);

foreach (Match match in regex.Matches(expressions))
{
      string expression = match.Value;

      using (System.Data.DataTable table = new System.Data.DataTable())
      {
           string result = table.Compute(expression, string.Empty).ToString();

           if (double.TryParse(result, out double res) && res <= 100)
           {
                validCombinations.Add(expression);
           }
      }
 }

 var final = string.Join(",", validCombinations);

If you have any question comment below :)

Rui Fernandes
  • 270
  • 1
  • 11
  • I´m sorry, but I´dont understand how can i use this example. Can you give me some advice? – Fernando Aug 16 '18 at 14:04
  • Don't you want to remove operations which result is bigger than 100 for example? In this example, for a given input of "(4*20+4*5), (3*20+7*5), (9*10+1*5), (9*10+10*5)" the output is "(4*20+4*5), (3*20+7*5), (9*10+1*5)". Isn't that what you want? – Rui Fernandes Aug 16 '18 at 14:14
  • No, what i want is all possible combinations under 100 and, (supposing to be coins), combinations with no more than 10 coins. – Fernando Aug 16 '18 at 14:28
  • Oh sorry, I didn't understand that. Do you really want all possible combinations which result is under 100? Can you imagine the number of possibilities of that? I don't know what's the problem that you want to solve with that but maybe you need to rethink that solution. – Rui Fernandes Aug 16 '18 at 14:37
  • If the available valor of coins are (5,10,20) and i want to pay 100, the combinations with no more than 10 coins are few. – Fernando Aug 16 '18 at 14:41
  • Oh I understood what you intend to do. It's like a exchange machine right? If you only have coins of 5, 10 and 20 and you need to give 100 you can do it returning 5 coins of 20, it's that? – Rui Fernandes Aug 16 '18 at 14:46
  • And you have a lot of possibilities for returning 100 like: - 20 coins of 5 - 10 coins of 10 - 5 coins of 20 - 1 coin of 20 + 1 coin of 10 + 14 coins of 5 ..... – Rui Fernandes Aug 16 '18 at 14:47
  • @Fernando check [this](https://stackoverflow.com/questions/28916675/coin-change-in-c-sharp-with-limited-coins#28916834) post. – Rui Fernandes Aug 16 '18 at 14:51
  • But in you example, i don´t want 14 coins of 5. Number of coins mustn't exceed 10. – Fernando Aug 16 '18 at 14:52
  • @Fernando Check the link in my previous comment, they solve almost all of your problem. You just need to implement that limit of 'X' coins :) – Rui Fernandes Aug 16 '18 at 15:04