public bool contains(NewPartyWithId obj) { if (this.buyers.IndexOf(obj) != -1) { return(true); } return(false); }
public void ReadFromFile() { //string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\Documents\TestCases2.txt"); string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\Documents\SingleTestCase.txt"); int noOfTestcases = Int32.Parse(lines[0]); int lineNoForNoOfBuyerSeller = 1; line = $" ---No of Testcases: {noOfTestcases}--- "; Console.WriteLine(line); file.WriteLine(line); int testcaseNo = 1; while (lineNoForNoOfBuyerSeller < lines.Length) { Buyers myBuyers = new Buyers(); Sellers mySellers = new Sellers(); line = $"{testcaseNo++}) TestCase ------ "; Console.WriteLine(line); file.WriteLine(line); String[] parsing = lines[lineNoForNoOfBuyerSeller].Split(' '); int noOfBuyers = Int32.Parse(parsing[0]); int noOfSellers = Int32.Parse(parsing[1]); for (int i = lineNoForNoOfBuyerSeller + 1; i <= lineNoForNoOfBuyerSeller + noOfBuyers; i++) { String[] buyerParsing = lines[i].Split(' '); NewPartyWithId myNewParty = new NewPartyWithId(Int32.Parse(buyerParsing[0]), Int32.Parse(buyerParsing[1]), Int32.Parse(buyerParsing[2]), buyerParsing[3]); myBuyers.addBuyers(myNewParty); } for (int j = lineNoForNoOfBuyerSeller + noOfBuyers + 1; j <= lineNoForNoOfBuyerSeller + noOfBuyers + noOfSellers; j++) { String[] sellerParsing = lines[j].Split(' '); NewPartyWithId myNewParty = new NewPartyWithId(Int32.Parse(sellerParsing[0]), Int32.Parse(sellerParsing[1]), Int32.Parse(sellerParsing[2]), sellerParsing[3]); mySellers.addSellers(myNewParty); } Buyers myCopyBuyers = new Buyers(); Sellers myCopySellers = new Sellers(); myCopyBuyers.setBuyers(mySellers.getCopySellers()); myCopySellers.setSellers(myBuyers.getCopyBuyers()); line = " Before Processing "; Console.WriteLine(line); file.WriteLine(line); printList(myBuyers, mySellers, true); line = " After Processing "; Console.WriteLine(line); file.WriteLine(line); solve(myBuyers, mySellers); printList(myBuyers, mySellers, true); line = " --- Reverse Result ---"; Console.WriteLine(line); file.WriteLine(line); solve(myCopyBuyers, myCopySellers); printList(myBuyers, mySellers, true); lineNoForNoOfBuyerSeller += noOfBuyers + noOfSellers + 1; } }
public void addBuyers(NewPartyWithId node) { if (this.hashSet.Contains(node.userId)) { this.addChild(node.userId, node); } else { this.hashSet.Add(node.userId); this.buyers.Add(node); } }
public void addChild(String uid, NewPartyWithId child) { int totalQtyTillNow = 0; int a = 0, range = 0; foreach (NewPartyWithId item in buyers) { if (item.userId == uid) { totalQtyTillNow += item.qty; item.isChild = true; a = item.a; range = item.range; } } child.totalQtyTillNow += totalQtyTillNow; child.isChild = true; buyers.Add(child); child.a = a; child.range = range; }
public Boolean subset_sum(int remaining_amount, int starting_index, List <NewPartyWithId> list, NewPartyWithId source, int source_index, List <NewPartyWithId> source_list) { if (remaining_amount == 0) { source.qty = 0; return(true); } else if (remaining_amount >= source.range && source.a == 0 && starting_index >= list.Count || source_index >= source_list.Count) { source.qty = remaining_amount; return(true); } if ((source.range != 0 && remaining_amount < source.range) || starting_index >= list.Count || source_index >= source_list.Count || remaining_amount < 0) { return(false); } Boolean result_1 = false; Boolean result_2 = false; int tempQuantity = list[starting_index].qty; if (list[starting_index].qty != list[starting_index].totalQtyTillNow && list[starting_index].isChild && (source.range == 0 || source.range <= list[starting_index].qty) && (source.a == 0 || source.qty <= list[starting_index].totalQtyTillNow)) { if (list[starting_index].range == 0 && list[starting_index].a == 0 && (source.range == 0 || source.range <= list[starting_index].totalQtyTillNow)) { if (remaining_amount <= list[starting_index].totalQtyTillNow) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } else { int endQty = 1; if (source.range != 0) { endQty = source.range; } for (int i = list[starting_index].totalQtyTillNow; i >= endQty; i--) { result_1 = subset_sum(remaining_amount - i, starting_index + 1, list, source, source_index, source_list); if (result_1) { updatePastStack(list, i, list[starting_index].userId); source.qty = 0; return(true); } } } } else if (list[starting_index].range == 0 && list[starting_index].a != 0 && source.range <= list[starting_index].totalQtyTillNow) { if (remaining_amount == list[starting_index].totalQtyTillNow) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } else if (remaining_amount < list[starting_index].totalQtyTillNow) { if (subset_sum(list[starting_index].totalQtyTillNow - remaining_amount, 0, source_list, list[starting_index], starting_index, list)) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } } else { if (subset_sum(remaining_amount - list[starting_index].totalQtyTillNow, starting_index + 1, list, source, source_index, source_list)) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } } } else if (list[starting_index].range != 0 && list[starting_index].a == 0 && source.range <= list[starting_index].totalQtyTillNow) { if (remaining_amount >= list[starting_index].range) { if (remaining_amount <= list[starting_index].totalQtyTillNow) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } else { int endQty = 1; if (source.range != 0) { endQty = source.range; } for (int i = list[starting_index].totalQtyTillNow; i >= endQty; i--) { result_1 = subset_sum(remaining_amount - i, starting_index + 1, list, source, source_index, source_list); if (result_1) { updatePastStack(list, i, list[starting_index].userId); source.qty = 0; return(true); } } } } else if (list[starting_index].range != 0 && list[starting_index].a != 0 && source.range <= list[starting_index].totalQtyTillNow && remaining_amount >= list[starting_index].range) { if (remaining_amount == list[starting_index].totalQtyTillNow) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } else if (remaining_amount < list[starting_index].totalQtyTillNow) { if (subset_sum(list[starting_index].totalQtyTillNow - remaining_amount, 0, source_list, list[starting_index], starting_index, list)) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } } else { if (subset_sum(remaining_amount - list[starting_index].totalQtyTillNow, starting_index + 1, list, source, source_index, source_list)) { updatePastStack(list, remaining_amount, list[starting_index].userId); source.qty = 0; return(true); } } } } return(subset_sum(remaining_amount, starting_index + 1, list, source, source_index, source_list)); } else if (!list[starting_index].isChild) { if (list[starting_index].range == 0 && list[starting_index].a == 0 && (source.range == 0 || source.range <= list[starting_index].qty)) { if (remaining_amount <= list[starting_index].qty) { list[starting_index].qty -= remaining_amount; //updatePastStack(list,remaining_amount,list[start]) source.qty = 0; result_1 = true; } else { int endQty = 1; if (source.range != 0) { endQty = source.range; } for (int i = list[starting_index].qty; i >= endQty; i--) { list[starting_index].qty = tempQuantity - i; result_1 = subset_sum(remaining_amount - i, starting_index + 1, list, source, source_index, source_list); if (result_1) { break; } } } } else if (list[starting_index].range == 0 && list[starting_index].a != 0 && (source.range == 0 || source.range <= list[starting_index].qty)) { if (list[starting_index].qty <= remaining_amount) { list[starting_index].qty = 0; result_1 = subset_sum(remaining_amount - tempQuantity, starting_index + 1, list, source, source_index, source_list); } else { int tempQty = list[starting_index].qty; list[starting_index].qty -= remaining_amount; int tempSourceQty = source.qty; source.qty = 0; if (subset_sum(list[starting_index].qty, 0, source_list, list[starting_index], starting_index, list)) { list[starting_index].qty = 0; result_1 = true; } else { source.qty = tempSourceQty; list[starting_index].qty = tempQty; } } } else if (list[starting_index].range != 0 && list[starting_index].a != 0 && (source.range == 0 || source.range <= list[starting_index].qty)) { if (list[starting_index].qty <= remaining_amount) { list[starting_index].qty = 0; result_1 = subset_sum(remaining_amount - tempQuantity, starting_index + 1, list, source, source_index, source_list); } } else if (list[starting_index].range != 0 && list[starting_index].a == 0 && (source.range == 0 || source.range <= list[starting_index].qty)) { if (remaining_amount >= list[starting_index].range && remaining_amount <= list[starting_index].qty) { list[starting_index].qty -= remaining_amount; source.qty = 0; result_1 = true; } else if (remaining_amount >= list[starting_index].range) { int endQty = Math.Max(source.range, list[starting_index].range); if (endQty == 0) { endQty = 1; } for (int i = list[starting_index].qty; i >= endQty; i--) { list[starting_index].qty = tempQuantity - i; result_1 = subset_sum(remaining_amount - i, starting_index + 1, list, source, source_index, source_list); if (result_1) { break; } } } } if (result_1 == false) { list[starting_index].qty = tempQuantity; result_2 = subset_sum(remaining_amount, starting_index + 1, list, source, source_index, source_list); } return(result_1 || result_2); } return(false); }
public void consoleWorks() { Buyers myBuyers = new Buyers(); Sellers mySellers = new Sellers(); int input = default(int); int qty = 0; int a = 0; int r = 0; char readI = default(char); while (readI != 'q') { Console.WriteLine("Enter -999 to Stop"); Console.WriteLine("Buyers:"); while (input != -999) { String choice, uid; Console.WriteLine(" Is Parent or Child: Enter P for Parent and C for Child"); choice = Console.ReadLine(); if (choice == "C") { Console.WriteLine(" Enter Parent Node UserId: "); uid = Console.ReadLine(); } else { Console.WriteLine("Enter UserId"); uid = Console.ReadLine(); Console.WriteLine("Enter 1 for ALL or Nothing Condition ,0 for nothing"); a = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter Ticket Size"); r = Int32.Parse(Console.ReadLine()); } Console.WriteLine("Enter Qty"); qty = Int32.Parse(Console.ReadLine()); NewPartyWithId myNewParty = new NewPartyWithId(qty, a, r, uid); if (choice == "C") { myBuyers.addChild(uid, myNewParty); } else { myBuyers.addBuyers(myNewParty); } Console.WriteLine("Enter -999 to Stop,anyother number to continue"); input = Int32.Parse(Console.ReadLine()); } Console.WriteLine("Sellers:"); input = 0; while (input != -999) { Console.WriteLine("Enter -999 to Stop"); Console.WriteLine("Sellers:"); while (input != -999) { String choice, uid; Console.WriteLine(" Is Parent or Child: Enter P for Parent and C for Child"); choice = Console.ReadLine(); if (choice == "C") { Console.WriteLine(" Enter Parent Node UserId: "); uid = Console.ReadLine(); } else { Console.WriteLine("Enter UserId"); uid = Console.ReadLine(); Console.WriteLine("Enter 1 for ALL or Nothing Condition ,0 for nothing"); a = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter Ticket Size"); r = Int32.Parse(Console.ReadLine()); } Console.WriteLine("Enter Qty"); qty = Int32.Parse(Console.ReadLine()); NewPartyWithId myNewParty = new NewPartyWithId(qty, a, r, uid); if (choice == "C") { mySellers.addChild(uid, myNewParty); } else { mySellers.addSellers(myNewParty); } Console.WriteLine("Enter -999 to Stop,any other number to continue"); input = Int32.Parse(Console.ReadLine()); } Buyers myCopyBuyers = new Buyers(); Sellers myCopySellers = new Sellers(); myCopyBuyers.setBuyers(myBuyers.getCopyBuyers()); myCopySellers.setSellers(mySellers.getCopySellers()); myBuyers.printBuyers(); mySellers.printSellers(); Console.WriteLine(" --- Actual Result ---"); solve(myBuyers, mySellers); Console.WriteLine(" --- Reverse Result ---"); solve(myCopyBuyers, myCopySellers); readI = (char)Console.ReadLine().ToCharArray()[0]; } } }