c# - Finding combinations from a list of objects and sorting them according to property value -
i have list of type cardslot, each of these cardslot objects contain different cards , , card1, card2, card3 (the number may differ).
each cardslot object has maximum quantity of card1, card2 , card3 pre-set. now, user inputs amount of cards required , should combination of cardslot fulfill criteria.
for eg:
list<cardslot> listcardslot = new list<cardslot>(); cardslot cardslot1 = new cardslot(); cardslot1.name = "cardslot1"; cardslot1.price = 900; cardslot1.card1 = 2; cardslot1.card2 = 3; cardslot1.card3 = 4; listcardslot.add(cardslot1); cardslot cardslot2 = new cardslot(); cardslot2.name = "cardslot2"; cardslot2.price = 850; cardslot2.card1 = 3; cardslot2.card2 = 2; cardslot2.card3 = 4; listcardslot.add(cardslot2); cardslot cardslot3 = new cardslot(); cardslot3.name = "cardslot3"; cardslot3.price = 950; cardslot3.card1 = 4; cardslot3.card2 = 3; cardslot3.card3 = 2; listcardslot.add(cardslot3); now, if user inputs card1 = 4, card2 = 5 , card3 = 4, end result should combination of cardslot objects least possible price.
can please give me nudge in right direction?kindly tell me if unclear , i'll try , improve it.
edit
i have tried finding possible combinations can in list using following function:
public static list<list<t>> itemcombinations<t>(list<t> inputlist, int minimumitems = 1) { int nonemptycombinations = (int)math.pow(2, inputlist.count) - 1; list<list<t>> listofcombinations = new list<list<t>>(nonemptycombinations + 1); if (minimumitems == 0) listofcombinations.add(new list<t>()); (int = 1; <= nonemptycombinations; i++) { list<t> thiscombination = new list<t>(inputlist.count); (int j = 0; j < inputlist.count; j++) { if ((i >> j) % 2 != 0) thiscombination.add(inputlist[j]); } if (thiscombination.count >= minimumitems) listofcombinations.add(thiscombination); } return listofcombinations; } this returns possible combinations. however, still not take care of situations repeated occurance of same card slot may correct choice. e.g in above mentioned scenario correct choice 1 x cardslot1 + 1 x cardslot2
edit 2
i reached intermediate solution , have posted answer, still not give me required answer in case of mix , match combinations. take , suggest towards end ?
so have made solution this, must it's headburn me, have tried something.
cardslot class made:
public class cardslot { public string name { get; set; } = ""; public double price { get; set; } = 0; public int card1 { get; set; } = 0; public int card2 { get; set; } = 0; public int card3 { get; set; } = 0; } and rest follows as:
//your code //as above //in question listcardslot.add(cardslot3); //your code valuechecker(1); double lowestamount = mycards.any() ? mycards.min(item => item.price) : 0; messagebox.show("you need " + multiplier.tostring() + " " + returnlowestcardslot(lowestamount).name); } int = 4, b = 5, c = 4; //user entered values int multiplier = 0; list<cardslot> mycards = new list<cardslot>(); cardslot returnlowestcardslot(double lowestprice) { cardslot cardslot = null; foreach (var item in mycards) { if (item.price == lowestprice) { cardslot = item; break; } } return cardslot; } void valuechecker(int increment) { multiplier += increment; foreach (cardslot item in listcardslot) { if (((multiplier * item.card1) >= a) && ((multiplier * item.card2) >= b) && ((multiplier * item.card3) >= c)) { if (!(mycards.contains(item))) mycards.add(item); } } if (mycards.count == 0) valuechecker(1); } the output i'm getting is:
you need 2 cardslot1
hope tweak 1 somehow fit need
edit
see want output 2 cardslot2 has :
cardslot2.card1 = 3; cardslot2.card2 = 2; cardslot2.card3 = 4; 2 x cardslot2 give:
cardslot2.card1 = 6; cardslot2.card2 = 4; cardslot2.card3 = 8; but user's requirement usercard1 = 4 , usercard1 = 5 & usercard1 = 4.
here card2 = 4 still lesser usercard2 = 5
don't see how fulfils requirement.
edit 2
your comment right, didn't though of that. give result:
//codes same //as question valuechecker(1); double lowestamount = mycards.any() ? mycards.min(item => item.price) : 0; cardslot leastcard = returnlowestcardslot(lowestamount, mycards); double lowestamount2 = listcardslot.any() ? listcardslot.min(item => item.price) : 0; cardslot lowestcard = returnlowestcardslot(lowestamount2, listcardslot); if ((leastcard.price + lowestcard.price) > (multiplier * leastcard.price)) messagebox.show("you need " + multiplier.tostring() + " " + leastcard.name); else messagebox.show("you need 1 " + leastcard.name + " , 1 " + lowestcard.name); } and modified returnlowestcardslot
cardslot returnlowestcardslot(double lowestprice, list<cardslot> list) { cardslot cardslot = null; foreach (var item in list) { if (item.price == lowestprice) { cardslot = item; break; } } return cardslot; } output:
you need 1 cardslot1 , 1 cardslot2
Comments
Post a Comment