Slicing Routine!

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

Hi All, I am looking for a smart solution to accomplish this task (.net 1.0)
Appreciate your input

I have a group of numbers in an arrayList
2,3,5,2,1,2,2

I need to output them into groups of
2,3,5
2
1
2
2

basically whenever I encounter 1 or 2 I bundle them up..

The numbers are in an arrayList, and I output them as string []

Arraylist Bundle(Arraylist)
{
// arr1 will contain (2,3,5)
alReturn.Add(arr1);
...................
return alReturn
}

TIA
 
So whenever you get a 2 or a 1 you start a new bundle?

Just if statement it then a bit like this, very rough code:

foreach(int i in arr1)
{
if ( i>0 && i <= 2)
{
//check string array if its empty make a new one
//if it isnt add it to array and start new one
//add 1 or 2 to string array
}
else
{
//concat to string array all numbers after 1 or 2
}
}

return completeStringArray;
 
Two approaches here, the first returns an arraylist of the starting
points of each segment, and the second returns an arraylist of
arraylists containing each segment.

private void button1_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();

int[] i = new int[] {2,3,5,2,1,2,2,3};
a.AddRange(i);
ArrayList b = DoIt2(a);
ArrayList c = DoIt(a);
Console.WriteLine(".");
}
private ArrayList DoIt(ArrayList pList)
{
ArrayList a = new ArrayList();
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;
}
private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
 
And here's the version without all the spurious extra code :)

private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}


Two approaches here, the first returns an arraylist of the starting
points of each segment, and the second returns an arraylist of
arraylists containing each segment.

private void button1_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();

int[] i = new int[] {2,3,5,2,1,2,2,3};
a.AddRange(i);
ArrayList b = DoIt2(a);
ArrayList c = DoIt(a);
Console.WriteLine(".");}private ArrayList DoIt(ArrayList pList)
{
ArrayList a = new ArrayList();
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;

Vai2000 said:
Hi All, I am looking for a smart solution to accomplish this task (.net 1.0)
Appreciate your input
I have a group of numbers in an arrayList
2,3,5,2,1,2,2
I need to output them into groups of
2,3,5
2
1
2
2
basically whenever I encounter 1 or 2 I bundle them up..
The numbers are in an arrayList, and I output them as string []
Arraylist Bundle(Arraylist)
{
// arr1 will contain (2,3,5)
alReturn.Add(arr1);
..................
return alReturn
}TIA- Hide quoted text -- Show quoted text -
 
Anyone that names their method "DoIt" deserves respect.


DeveloperX said:
And here's the version without all the spurious extra code :)

private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}


Two approaches here, the first returns an arraylist of the starting
points of each segment, and the second returns an arraylist of
arraylists containing each segment.

private void button1_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();

int[] i = new int[] {2,3,5,2,1,2,2,3};
a.AddRange(i);
ArrayList b = DoIt2(a);
ArrayList c = DoIt(a);
Console.WriteLine(".");}private ArrayList DoIt(ArrayList pList)
{
ArrayList a = new ArrayList();
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;

Vai2000 said:
Hi All, I am looking for a smart solution to accomplish this task (.net
1.0)
Appreciate your input
I have a group of numbers in an arrayList
2,3,5,2,1,2,2
I need to output them into groups of
2,3,5
2
1
2
2
basically whenever I encounter 1 or 2 I bundle them up..
The numbers are in an arrayList, and I output them as string []
Arraylist Bundle(Arraylist)
{
// arr1 will contain (2,3,5)
alReturn.Add(arr1);
..................
return alReturn
}TIA- Hide quoted text -- Show quoted text -
 
Sorry to discourage....but all the answers are spurious...!

Input ArrayList:
2,3,4,20
1,2,2
2
2,2,2
1,2,3,4
1,1
null

expected oututs
Bundle Count=1 and the bundle will contain 2,3,4,20
Bundle Count=3 Bundle#1 wil contain 1 Bundle#2 will contain 2 and Bundle#3
will cotain 2
Bundle Count=1 Bundle#1 will contain 2
Bundle Count=3 Bundle#1 wil contain 2 Bundle#2 will contain 2 and Bundle#3
will cotain 2
Bundle Count=2 Bundle#1 wil contain 1 Bundle#2 will contain 2 ,3,4
{formatted with,}
Bundle Count =2 Bundle#1 wil contain 1 Bundle#2 will contain 1
Null
............






Daniel said:
Anyone that names their method "DoIt" deserves respect.


DeveloperX said:
And here's the version without all the spurious extra code :)

private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}


Two approaches here, the first returns an arraylist of the starting
points of each segment, and the second returns an arraylist of
arraylists containing each segment.

private void button1_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();

int[] i = new int[] {2,3,5,2,1,2,2,3};
a.AddRange(i);
ArrayList b = DoIt2(a);
ArrayList c = DoIt(a);
Console.WriteLine(".");}private ArrayList DoIt(ArrayList pList)
{
ArrayList a = new ArrayList();
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;




Hi All, I am looking for a smart solution to accomplish this task (.net
1.0)
Appreciate your input

I have a group of numbers in an arrayList
2,3,5,2,1,2,2

I need to output them into groups of
2,3,5
2
1
2
2

basically whenever I encounter 1 or 2 I bundle them up..

The numbers are in an arrayList, and I output them as string []

Arraylist Bundle(Arraylist)
{
// arr1 will contain (2,3,5)
alReturn.Add(arr1);
..................
return alReturn

}TIA- Hide quoted text -- Show quoted text -
 
I also have JustDoIt for when it's really time critical ;)

@Vai, I tested it with your set and a couple of others and it worked
as expected. I did make a few assumptions like your string would start
with a 1 or 2. Sorry it didn't help.


Anyone that names their method "DoIt" deserves respect.

DeveloperX said:
And here's the version without all the spurious extra code :)
private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
Two approaches here, the first returns an arraylist of the starting
points of each segment, and the second returns an arraylist of
arraylists containing each segment.
private void button1_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();
int[] i = new int[] {2,3,5,2,1,2,2,3};
a.AddRange(i);
ArrayList b = DoIt2(a);
ArrayList c = DoIt(a);
Console.WriteLine(".");}private ArrayList DoIt(ArrayList pList)
{
ArrayList a = new ArrayList();
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;
Hi All, I am looking for a smart solution to accomplish this task (.net
1.0)
Appreciate your input
I have a group of numbers in an arrayList
2,3,5,2,1,2,2
I need to output them into groups of
2,3,5
2
1
2
2
basically whenever I encounter 1 or 2 I bundle them up..
The numbers are in an arrayList, and I output them as string []
Arraylist Bundle(Arraylist)
{
// arr1 will contain (2,3,5)
alReturn.Add(arr1);
..................
return alReturn
}TIA- Hide quoted text -- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top