Hello, I have problem that I need to solve.
I have 3 string "a", "b" and "c".
I need all varabiles to be added in listbox.
Sample of list:
aaa
aab
aac
abc
abb
acc
acb
baa
bba
bca
caa
cba
cca
cac
bac
etc.
Can someone show me some code for this problem ?
Loading

FroglegRank #1,020 · 1.9K reputationPosted Feb 23, 2011, 10:00 AM EST
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Add("aaa");
listBox1.Items.Add("ccc");
listBox1.Items.Add("bbb");
char[] abc = new char[3]{'a','b','c'};
setper(abc);
char[] aab = new char[3] { 'a', 'a', 'b' };// you will need all char combinations eg aac,bba etc
setper(aab);
listBox1.Sorted = true;
//label1.Text = Convert.ToString(listBox1.Items.Count);
}
private void swap(ref char a, ref char b)
{
if (a == b)return;
a ^= b;
b ^= a;
a ^= b;
}
public void setper(char[] list)
{
int x = list.Length - 1;
go(list, 0, x);
}
private void go (char[] list, int k, int m)
{
int i;
if (k == m)
{
if (listBox1.Items.Contains(list[0].ToString() + list[1].ToString() + list[2].ToString()) == false)
{
listBox1.Items.Add(list[0].ToString() + list[1].ToString() + list[2].ToString());
}
}
else
for (i = k; i <= m; i++)
{
swap (ref list[k],ref list[i]);
go (list, k+1, m);
swap (ref list[k],ref list[i]);
}
}
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 3:51 AM EST
Janis Peksa145 reputationPosted Feb 21, 2011, 3:52 AM EST
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 11:46 AM EST
listBox1.Items.Add("aaa");
listBox1.Items.Add("aab");
listBox1.Items.Add("aac");
listBox1.Items.Add("aba");
listBox1.Items.Add("abb");
listBox1.Items.Add("abc");
listBox1.Items.Add("aca");
listBox1.Items.Add("acb");
listBox1.Items.Add("acc");
listBox1.Items.Add("baa");
listBox1.Items.Add("bab");
listBox1.Items.Add("bac");
listBox1.Items.Add("bba");
listBox1.Items.Add("bbb");
listBox1.Items.Add("bbc");
listBox1.Items.Add("bca");
listBox1.Items.Add("bcb");
listBox1.Items.Add("caa");
listBox1.Items.Add("cab");
listBox1.Items.Add("cac");
listBox1.Items.Add("cba");
listBox1.Items.Add("cbb");
listBox1.Items.Add("cca");
listBox1.Items.Add("ccc");
listBox1.Sorted = true;
is less code
Janis Peksa145 reputationPosted Feb 21, 2011, 9:38 PM EST
If I will have 5 or 10 symbols ?
Need somekind of generator or command to do this.
Janis Peksa145 reputationPosted Feb 22, 2011, 9:36 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 23, 2011, 9:41 AM EST
Janis Peksa145 reputationPosted Feb 23, 2011, 9:49 AM EST
What do you not understand ?
I need in all combinations 'a', 'b' and 'c'
to be added in listbox.
Suthish NairRank #92 · 22.8K reputationPosted Feb 23, 2011, 10:18 AM EST
After Frogleg given the solution, your query changed to something else symbols.
So why not asked in first thread itself.
Thats why i said, your question is not clear.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 23, 2011, 4:41 PM EST
char[] ca = { 'a', 'b', 'c' };
String Permutation;
foreach (char c1 in ca)
foreach (char c2 in ca)
foreach (char c3 in ca)
Permutation = String.Format("{0} {1} {2}", c1, c2, c3);
Janis Peksa145 reputationPosted Feb 23, 2011, 10:37 PM EST
Ops, I realy didn't try a code. [Edited]
FroglegRank #1,020 · 1.9K reputationPosted Feb 23, 2011, 11:22 PM EST
Janis Peksa145 reputationPosted Feb 23, 2011, 11:54 PM EST
Janis Peksa145 reputationPosted Feb 28, 2011, 4:47 AM EST