Smart Slicing Routine!!

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

Vai2000

Hi All, I need some smart way to accomplish this task in C# (.net 1.0)

Appreciate your inputs!!
==================================
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
............
==================================
 
The answers in the other thread will do what you are after. Though seeing
this it was a poor explanation in your original post. And this one isn't too
clear either. So you have a load of array lists each with data and you want
them to be broken up into new arrays of data? Or do you want them broken up
into individual strings? strings arrays? What?

Anyway this is a guide, you do need to apply some coding skill to match your
exact requirement as no one helping knows the context of all this, so:

1) Loop your input array list
2) Get the value of an array slot and check if it is 2 or 1
3) if 2 or 1 create new array, string array whatever it is you want it as,
store it in the array
4) go to next slot, if it is not a 2 or 1 append to the created array in
step 3.
5) If it is a 2 or 1 store the array created and make a new one, store the
2/1 and repeat.

Now do that for every array list being passed in. That will work. Up to you
to apply it. If you struggle to apply it then try and post your code
attempt.
 
I am looking for a smart solution, I have already done it but its not a very
smart solution!!
Answers to your questions:
1. The ArrayList stores strings inside it.....
so what I showed below are contents of the arrayList...

I need tight routine!!!

TIA
 
Hi,

What exactly was not smart about the solution presented in the other
thread?

Brian
 
Sadly No! Dont worry about it...I finally wrote it myself
===================================

for(int idx=0;idx<alNonBundle.Count;idx++)
{
str=alNonBundle[idx];

if(str.Equals("2") || str.Equals("1"))
{
pos=idx;

CopyArray(lastpos,pos,alNonBundle);

lastpos=pos;
}

}
void CopyArray(int spos,int epos,ArrayList al)
{
string []arr=new string[epos-spos];
al.CopyTo(spos,arr,0,epos-spos);
}
 
Hi,

Are you sure that's right? That code doesn't produce any side
effects. Seems like the following is more of what you want.

public IList Partition(IList input)
{
IList output = new ArrayList();
IList bundle = new ArrayList();
foreach (string item in input)
{
if (bundle.Count > 0 && (item == "1" || item == "2"))
{
output.Add(bundle);
bundle = new ArrayList();
}
bundle.Add(item);
}
output.Add(bundle);
return output;
}

Brian
 
You actually tried? For yourself! Gasp! Surely not..... Word of advice and
friendly tip. Don't use exclamation marks when asking for help, try not to
be rude about help you are given and be respectful of others.



Vai2000 said:
Sadly No! Dont worry about it...I finally wrote it myself
===================================

for(int idx=0;idx<alNonBundle.Count;idx++)
{
str=alNonBundle[idx];

if(str.Equals("2") || str.Equals("1"))
{
pos=idx;

CopyArray(lastpos,pos,alNonBundle);

lastpos=pos;
}

}
void CopyArray(int spos,int epos,ArrayList al)
{
string []arr=new string[epos-spos];
al.CopyTo(spos,arr,0,epos-spos);
}



Brian Gideon said:
Hi,

What exactly was not smart about the solution presented in the other
thread?

Brian
 

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