[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Looping through the dataset, vb.net

Posted on 2007-10-12
10
Medium Priority
?
756 Views
Last Modified: 2013-11-07
Hello,

I have a dataset and following are the values with two columns:

column1       column2

Gift1            1,000
Gift2            2,000
Gift3            3,000
Gift4            4,000
Gift5           10,000


The customer has the amount of 14,500

I need to loop through the dataset and display to the user saying:

we can accomodate only

1.  Gift 5 - 10,000
2.  Gift 4 - 4,000

This is (14,000 which is less than 14,500)


(we can also say that we can accomodate, gift1, gift3 and gift5, which sums to (14,000 < 14,500), but i have to check if the highest value is less than the given amount(14,500) and assign from there)

Any help would be appreciated

Thank you,
anagm
0
Comment
Question by:anagm
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20066318
Are you looking at displaying all possibilities of gifts that can be accomodated by 14500?
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 672 total points
ID: 20066397
I would suggest sorting the dataset based on the amount in descending/descending order first and keep adding the values untill the value is less than 14500. displaying the gifts untill the sum became >= 14500

0
 
LVL 20

Assisted Solution

by:REA_ANDREW
REA_ANDREW earned 664 total points
ID: 20066555
I am getting close with the following I have made

    class ProbGenerator
    {
        private List<Int32> values;
        private Int32 maxValue;
        private StringBuilder sb1;

        public ProbGenerator(Int32 _maxValuem, params Int32[] inValues)
        {
            values = new List<int>(inValues);
            maxValue = _maxValuem;
            sb1 = new StringBuilder();
        }
        public void Start()
        {
            for (int i = 0; i < values.Count; i++)
            {
                sb1.AppendLine();
                int Total = values[i];
                sb1.Append(values[i].ToString() + ":");
                for (int j = 0; j < values.Count; j++)
                {
                    if (j != i)
                    {
                        Total += values[j];
                        if ((Total) <= maxValue)
                        {
                            sb1.Append(values[j].ToString() + ":");
                        }
                        else
                        {
                            Total -= values[j];
                        }
                    }
                }
            }
            Console.WriteLine(sb1.ToString());
        }
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20066572
I started like this

            ProbGenerator p1 = new ProbGenerator(14500, 1000, 2000, 3000, 4000, 10000);
            p1.Start();

And the output is like this

1000:2000:3000:4000
2000:1000:3000:4000
3000:1000:2000:4000
10000:1000:2000
0
 

Author Comment

by:anagm
ID: 20066576
Hi surajguptha:

can you send me a sample code to do the above suggested solution.

anagm
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20066587
To get all possibilities though it would have to get much more complicated as i could also have like you said 1,3 and 5. BUT if it had bypassed 1000, and 2000 and that last run it would have seen that it could use 3000, and then seen it can use 4000.  hmmm. good question this one lol

Andrew :-)
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20068590
Thanks anagm - This was an interesting problem! Here is one solution.

1. Define the Gift class
public class Gift
{
   private string mName = string.Empty;
   private int mValue = 0;
   private bool mBlocked = false;
   public bool Blocked
   {
      get
      {
         return mBlocked;
      }
      set
      {
         mBlocked = value;
      }
   }
   public string Name
   {
      get
      {
         return mName;
      }
      set
      {
         mName = value;
      }
   }
   public int Value
   {
      get
      {
         return mValue;
      }
      set
      {
         mValue = value;
      }
   }
   public Gift(string name, int value)
   {
      mName = name;
      mValue = value;
   }
}

Then in the form class:

private string GetCombinations(int budget, List<Gift> gifts)
{
   StringBuilder sb = new StringBuilder(1024);

   // First, sort by value
   List<Gift> ordered = OrderGiftsByDecreasingValue(gifts);

   // Next, skip any individuals greater than the budget
   int topIndex = 0;
   while ((topIndex < ordered.Count) && (ordered[topIndex].Value > budget))
      topIndex++;

   // If all are out of reach, let them know.
   if (topIndex >= ordered.Count)
      return "You cannot afford any of the gifts in the list.";

   // Next, generate all distinct combinations of individuals where the total
   // value is less than the max.
   int currentTotal;
   int currentIndex;
   bool skippedOne = true;
   int firstSkipped = -1;
   int firstBlocked = -1;
   bool blockedOne = false;
   sb.Append("The following combinations of gifts are within your budget\n");
   while ((topIndex < ordered.Count) && (skippedOne || blockedOne))
   {
      currentIndex = topIndex + 1;
      firstSkipped = -1;
      currentTotal = ordered[topIndex].Value;
      sb.Append(ordered[topIndex].Name + ":");
      while (currentIndex < ordered.Count)
      {
         if (!ordered[currentIndex].Blocked)
         {

            if ((currentTotal + ordered[currentIndex].Value) < budget)
            {
               currentTotal += ordered[currentIndex].Value;
               sb.Append(ordered[currentIndex].Name + ":");
            }
            else
            {
               if (firstSkipped == -1)
                  firstSkipped = currentIndex;
            }
         }
         currentIndex++;
      }
      sb.Append("\n");  // Finished one
      skippedOne = (firstSkipped > -1);
      blockedOne = (firstBlocked > -1);
      if (skippedOne) // Adjust blocks
      {
         if ((firstSkipped - 1) == topIndex) // Set completed - next pass start with the next one down
         {
            topIndex++;
            blockedOne = false;
            skippedOne = false;
            firstBlocked = -1;
         }
         else // Block the one prior to the skip
         {
            ordered[firstSkipped - 1].Blocked = true;
            blockedOne = true;
         }
      }
      else // None were skipped
      {
         // If any were blocked, move the block start up one and clear blocking flags
         if (blockedOne)
         {
            if ((firstBlocked - 1) == topIndex)
            {
               topIndex++;
               blockedOne = false;
               firstBlocked = -1;
            }
            else // block the next one up and clear the blocked flags below that
            {
               firstBlocked--;
               for (int i = firstBlocked + 1; i < ordered.Count; i++)
                  ordered[i].Blocked = false;
            }
         }
         else // None skipped or blocked
            topIndex++;
      }
   }
   return sb.ToString();
}
private List<Gift> OrderGiftsByDecreasingValue(List<Gift> gifts)
{
   List<Gift> orderedList = new List<Gift>(gifts.Count);
   Gift current = null;
   bool inserted = false;
   orderedList.Add(gifts[0]);
   int index = 1;
   int orderedIndex = 0;

   while (index < gifts.Count)
   {
      inserted = false;
      current = gifts[index++];
      orderedIndex = 0;
      while (!inserted && orderedIndex < orderedList.Count)
      {
         if (current.Value > orderedList[orderedIndex].Value)
         {
            orderedList.Insert(orderedIndex, current);
            inserted = true;
         }
         orderedIndex++;
      }
      if (!inserted)
         orderedList.Add(current);
   }
   return orderedList;
}
private void Test_Click(object sender, EventArgs e)
{
   List<Gift> gifts = new List<Gift>(5);
   gifts.Add(new Gift("Gift1", 1000));
   gifts.Add(new Gift("Gift2", 2000));
   gifts.Add(new Gift("Gift3", 3000));
   gifts.Add(new Gift("Gift4", 4000));
   gifts.Add(new Gift("Gift5", 10000));
   textBox1.Text = GetCombinations(14500, gifts);
}

Clicking the button produced the following:

The following combinations of gifts are within your budget
Gift5:Gift4:
Gift5:Gift3:Gift1:
Gift5:Gift2:Gift1:


Jim
0
 
LVL 22

Assisted Solution

by:JimBrandley
JimBrandley earned 664 total points
ID: 20071527
After lookig at this a bit more, I noticed that my main method skipped one combination. Here's the corrected method.

Jim

private string GetCombinations(int budget, List<Gift> gifts)
{
   StringBuilder sb = new StringBuilder(1024);

   // First, sort by value
   List<Gift> ordered = OrderGiftsByDecreasingValue(gifts);

   // Next, skip any individuals greater than the budget
   int topIndex = 0;
   while ((topIndex < ordered.Count) && (ordered[topIndex].Value > budget))
      topIndex++;

   // If all are out of reach, let them know.
   if (topIndex >= ordered.Count)
      return "You cannot afford any of the gifts in the list.";

   // Next, generate all distinct combinations of individuals where the total
   // value is less than the max.
   int currentTotal;
   int currentIndex;
   bool skippedOne = true;
   int firstSkipped = -1;
   int firstBlocked = -1;
   bool blockedOne = false;
   sb.Append("The following combinations of gifts are within your budget\n");
   while ((topIndex < ordered.Count) && (skippedOne || blockedOne))
   {
      currentTotal = ordered[topIndex].Value;
      currentIndex = topIndex + 1;
      // Need to block any at the top of the list that cannot be added
      while ((currentTotal + ordered[currentIndex].Value) > budget)
      {
         if (firstBlocked == -1)
            firstBlocked = currentIndex;
         blockedOne = true;
         ordered[currentIndex++].Blocked = true;
      }
      firstSkipped = -1;
      sb.Append(ordered[topIndex].Name + ":");
      while (currentIndex < ordered.Count)
      {
         if (!ordered[currentIndex].Blocked)
         {

            if ((currentTotal + ordered[currentIndex].Value) <= budget)
            {
               currentTotal += ordered[currentIndex].Value;
               sb.Append(ordered[currentIndex].Name + ":");
            }
            else
            {
               if (firstSkipped == -1)
                  firstSkipped = currentIndex;
            }
         }
         currentIndex++;
      }
      sb.Append(" Total for set = " + currentTotal + "\n");  // Finished one
      skippedOne = (firstSkipped > -1);
      blockedOne = (firstBlocked > -1);
      if (skippedOne) // Adjust blocks
      {
         if ((firstSkipped - 1) == topIndex) // Set completed - next pass start with the next one down
         {
            topIndex++;
            blockedOne = false;
            firstBlocked = -1;
            for (int i = firstBlocked + 1; i < ordered.Count; i++)
               ordered[i].Blocked = false;
         }
         else // Block the one prior to the skip
         {
            if (firstBlocked == -1)
            {
               ordered[firstSkipped - 1].Blocked = true;
               firstBlocked = firstSkipped - 1;
            }
            else // Block the next one down that's not blocked
            {
               currentIndex = firstBlocked;
               while (ordered[currentIndex].Blocked)
                  currentIndex++;
               ordered[currentIndex].Blocked = true;
            }
            blockedOne = true;
         }
      }
      else // None were skipped
      {
         // If any were blocked, move the block start up one and clear blocking flags
         if (blockedOne)
         {
            if ((firstBlocked - 1) == topIndex)
            {
               topIndex++;
               skippedOne = true; // This keeps us in the main loop for another try.
               blockedOne = false;
               firstBlocked = -1;
            }
            else // block the next one up and clear the blocked flags below that
            {
               firstBlocked--;
               ordered[firstBlocked].Blocked = true;
            }
            for (int i = firstBlocked + 1; i < ordered.Count; i++)
               ordered[i].Blocked = false;
         }
         else // None skipped or blocked
            topIndex++;
      }
   }
   return sb.ToString();
}
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20285258
Forced accept.

Computer101
EE Admin
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question