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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Efficient Group Processing

This should be easy and want to try to get another way of doing this. I will not show my answer because I don't want to taint your way of thinking. I want to find an easy but efficient way to solve this issue.

In Parameters (2)

String[] s = {"KJDKF","SDJFK","JENN","HDEEJ","JSKE","UAJS"}
4 = Max items I want in each dimension

string[] result = Splitter(s,4);

Tho expected output:

result [0] = "KJDKF","SDJFK","JENN","HDEEJ"
result [1] = "JSKE","UAJS"

I currently have it going through 2 loops but I think there is a quicker way and my brain now hurts from other login I was doing.

Thanks

0
ad5qa
Asked:
ad5qa
  • 7
  • 5
  • 3
1 Solution
 
Rob SiklosCommented:
not tested, but here's how I would do it...
public string[][] Splitter(string[] s, int maxNum) {
 
  int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
  int remainder = numElements % maxNum;
 
  List<string[]> result = new List<string[]>(numResultElements);
 
  for (int i = 0; i < numResultElements; i++) {
     string[] currArray;
 
     int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;
 
     currArray = new string[numCurrElements];
     Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);
 
     result.Add(currArray);
  }
 
  return result.ToArray();
 
}

Open in new window

0
 
Rob SiklosCommented:
Sorry, ignore the above - try this:
public string[] Splitter(string[] s, int maxNum) {
 
  int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
  int remainder = numElements % maxNum;
 
  List<string> result = new List<string>(numResultElements);
 
  for (int i = 0; i < numResultElements; i++) {
     string[] currArray;
 
     int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;
 
     currArray = new string[numCurrElements];
     Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);
 
     result.Add(String.Join(",", currArray);
  }
 
  return result.ToArray();
 
}

Open in new window

0
 
anarki_jimbelCommented:
It's quite interestin but I came to a nearly same solution as rob263. Independently :)

My is checked.
    private void button1_Click(object sender, EventArgs e)
 
        {
 
            ////HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com/helloworld.xml");
            ////string result = ((HttpWebResponse)myRequest.GetResponse()).ToString();
            ////doc.LoadXml(result);
            //XmlNodeList greeting = doc.GetElementsByTagName("greeting");
            //MessageBox.Show(greeting[0].InnerXml);
 
            //if (radioButton1.Checked){
            //    MessageBox.Show("checked");
            //}
            //else{
            //    MessageBox.Show("not checked");
            //}
 
            String[] s = { "KJDKF", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS" };
            int limit = 4;
 
            List<string[]> splitted = splitter(s,limit);
        }
 
        private List<string[]> splitter(string[] origArray, int limit)
        {
            List<string[]> splittedArrays = new List<string[]>();
            string[] currentArray = null;
 
            for (int i = 0; i < origArray.Length; i++)
            {
                if ((i % limit) == 0)
                {
                    if ((origArray.Length - limit * splittedArrays.Count) >= limit)
                    {
                        currentArray = new string[limit];
                    }
                    else
                    {
                        currentArray = new string[origArray.Length - limit * splittedArrays.Count];
                    }
 
                    splittedArrays.Add(currentArray);
                }
                currentArray[i - (splittedArrays.Count - 1) * limit] = origArray[i];
            }
 
            return splittedArrays;
        }

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
anarki_jimbelCommented:
Sorry! Ignore this HTTP commented part - just some rubbish :)
0
 
ad5qaAuthor Commented:
Thats great, I will post one tomorrow that was suggested to me. The concept made since after I stepped back and then it hit my "why didn't I think of that" hopefully it will work.

Thanks for the input, will check them out tomorrow.
0
 
ad5qaAuthor Commented:
This is what I came up with ... Any other people want to submit an example?



	private static string[] SplitString(string sInput, int maxItemsInArray)
	{
		List<string> items = new List<string>();
		string[] splitter = sInput.Split(",".ToCharArray());
		int pos = 0;
		int group = maxItemsInArray;
 
		while (pos < splitter.Length)
		{
			if (pos < splitter.Length - maxItemsInArray && group < splitter.Length)
			{
				StringBuilder sbVal = new StringBuilder();
				for (int i = 0; i < maxItemsInArray; i++)
				{
					sbVal.Append(splitter[pos] + ",");
					pos++;
				}
				group += 100;
				items.Add(sbVal.ToString().Substring(0, sbVal.ToString().LastIndexOf(",")));
			}
			else
			{
				StringBuilder sbVal = new StringBuilder();
				for (int i = pos; i < splitter.Length; i++)
				{
					sbVal.Append(splitter[pos] + ",");
					pos++;
				}
				items.Add(sbVal.ToString().Substring(0, sbVal.ToString().LastIndexOf(",")));
			}
 
		}
 
		return items.ToArray();
	}

Open in new window

0
 
Rob SiklosCommented:
with all your use of stringbuilders and Substring(), I think your solution is less efficient.
0
 
ad5qaAuthor Commented:
I would tend to agree, thanks for the feedback. It's interesting finding different ways to do the same thing.
0
 
ad5qaAuthor Commented:
Here are the results that I recorded processing 247 GUIDS in groups of 50 max size:

My Code:
Start 0
Finish 7296576 ticks 2 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 7498099 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 784833 ticks 0 ms


0
 
ad5qaAuthor Commented:
Actually I needed to take the average of 5 different runs without debugging


My Code:
Start 0
Finish 15484805 ticks 5 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 6445026 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 620564 ticks 0 ms


0
 
Rob SiklosCommented:
Try the following - I think it might be even better.  The difference is that I no longer allocate a new array in every iteration of the loop.  Since memory allocation is a relatively slow operation, it will probably help.
public string[] Splitter(string[] s, int maxNum) {
 
  int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
  int remainder = numElements % maxNum;
  List<string> result = new List<string>(numResultElements);
  string[] currArray = new string[maxNum];
 
  for (int i = 0; i < numResultElements; i++) {
     int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;
 
     if (numCurrElements != maxNum) {
       currArray = new string[numCurrElements];
     }
 
     Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);
     result.Add(String.Join(",", currArray);
  }
  return result.ToArray();
}

Open in new window

0
 
Rob SiklosCommented:
There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0
 
Rob SiklosCommented:
There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0
 
anarki_jimbelCommented:
And it looks like rob's code is most efficient!
0
 
Rob SiklosCommented:
I recommend http:#23686840 - since that's what was used in the author's benchmark test and is an order of magnitude faster than the other solutions.

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now