Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Codeproject. I find a code in VB for generate all possible 4 Digit combination
and i tried to convert it to C#. It seems work well. but not eliminate the same combination. can anybody here fix my code...
C#
#region

using System;
using System.Collections;
using System.Windows.Forms;

#endregion

namespace WindowsFormApplication1
{
    public partial class Permutation : Form
    {
        private string _temporaryOutputString;

        private ArrayList _temporaryOutputArrayList;
        public Permutation()
        {
            InitializeComponent();
        }
        private void StoreCurrentNumericCode(ref ArrayList numericCodeArrayList)
        {
            _temporaryOutputString = numericCodeArrayList[0].ToString();
            _temporaryOutputString += numericCodeArrayList[1].ToString();
            _temporaryOutputString += numericCodeArrayList[2].ToString();
            _temporaryOutputString += numericCodeArrayList[3].ToString();
            _temporaryOutputString += Environment.NewLine;
            _temporaryOutputArrayList.Add(_temporaryOutputString);
        }

        private void RemoveNumericCodeDuplicates(ref ArrayList storedNumericCodeArrayList)
        {
            string currentlyEvaluatedElementText = "";
            bool IsReadLoopOfArraylistComplete = false;
            bool IsDuplicateRemovalLoopOfArraylistComplete = false;
            int storedNumericCodeElementIndex = 0;
            int duplicateSearchElementIndex = 0;

            while (!(storedNumericCodeElementIndex >= storedNumericCodeArrayList.Count))
            {
                currentlyEvaluatedElementText = storedNumericCodeArrayList[storedNumericCodeElementIndex].ToString();
                duplicateSearchElementIndex = storedNumericCodeElementIndex + 1;
                if (Convert.ToInt32(currentlyEvaluatedElementText) == 9139)
                {
                    int i = 1;
                }
                while (!(duplicateSearchElementIndex >= storedNumericCodeArrayList.Count))
                {
                    if ((storedNumericCodeArrayList[duplicateSearchElementIndex] == currentlyEvaluatedElementText))
                    {
                        storedNumericCodeArrayList.RemoveAt(duplicateSearchElementIndex);
                        duplicateSearchElementIndex = duplicateSearchElementIndex - 1;
                        if (!(storedNumericCodeElementIndex == 0))
                        {
                            storedNumericCodeElementIndex = storedNumericCodeElementIndex - 1;
                        }
                    }
                    duplicateSearchElementIndex += 1;
                }
                storedNumericCodeElementIndex += 1;
            }
        }
        private void OutputUniqueNumericCodes(ref ArrayList storedNumericCodeArrayList)
        {
            for (int storedNumericCodeElementIndex = 0; storedNumericCodeElementIndex <= storedNumericCodeArrayList.Count - 1; storedNumericCodeElementIndex++)
            {
                richTextBox1.Text += storedNumericCodeArrayList[storedNumericCodeElementIndex];
                //TextBox1.Text &= Environment.NewLine
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList numericCodeOutputArrayList = new ArrayList();
            string[] numericCodeInitArray = new string[4];
            numericCodeInitArray[0] = "9";
            numericCodeInitArray[1] = "1";
            numericCodeInitArray[2] = "3";
            numericCodeInitArray[3] = "9";

            numericCodeOutputArrayList.Add(numericCodeInitArray[0]);
            numericCodeOutputArrayList.Add(numericCodeInitArray[1]);
            numericCodeOutputArrayList.Add(numericCodeInitArray[2]);
            numericCodeOutputArrayList.Add(numericCodeInitArray[3]);

            string firstOutputElementText = "";
            string secondOutputElementText = "";
            string thirdOutputElementText = "";
            _temporaryOutputArrayList = new ArrayList();

            for (int repositionFirstIndex = 0; repositionFirstIndex <= 3; repositionFirstIndex++)
            {
                for (int repositionSecondIndex = 0; repositionSecondIndex <= 2; repositionSecondIndex++)
                {
                    for (int repositionThirdIndex = 0; repositionThirdIndex <= 1; repositionThirdIndex++)
                    {
                        StoreCurrentNumericCode(ref numericCodeOutputArrayList);

                        thirdOutputElementText = numericCodeOutputArrayList[2].ToString();
                        numericCodeOutputArrayList.RemoveAt(2);
                        numericCodeOutputArrayList.Add(thirdOutputElementText);
                    }

                    secondOutputElementText = numericCodeOutputArrayList[1].ToString();
                    numericCodeOutputArrayList.RemoveAt(1);
                    numericCodeOutputArrayList.Add(secondOutputElementText);
                }

                firstOutputElementText = numericCodeOutputArrayList[0].ToString();
                numericCodeOutputArrayList.RemoveAt(0);
                numericCodeOutputArrayList.Add(firstOutputElementText);
            }

            RemoveNumericCodeDuplicates(ref _temporaryOutputArrayList);
            OutputUniqueNumericCodes(ref _temporaryOutputArrayList);
        }
    }
}


For any attention from Codeproject and any member

Thx a Lot
Posted
Comments
AmitGajjar 17-Dec-11 8:09am    
hi,

can you show us some sample output you want to generate ? it will take time to understand existing code better to know about your desired output.

thanks
-amit
SunLucDong 17-Dec-11 14:34pm    
this is The output i want to generate
9139
9193
9391
9319
9913
9931
1399
1399
1993
1939
1939
1993
3991
3919
3919
3991
3199
3199
9913
9931
9139
9193
9391
9319

But it show two time a same number..
i want to eliminate the same number

2 points:
1) Did the VB original do what you wanted? If so, then your translation is faulty - try http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] It's an on-line translator which handles valid code pretty well.
2) Calling it "your code" when you got it elsewhere is a bit like calling it "my car" because I sat in it in the showroom...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Dec-11 18:41pm    
Good points, a 5.
--SA
Are you aware that regular permutations don't do duplicates? Your data set of 9,1,3,9 will generate 16 permutations from an actual possible 24. If that's indeed what you're expecting, your permutation code can be just 14 lines of code (accounting for proper code formatting techniques). Further, you can put the results into a HashSet to avoid duplicate checking (although, even that isn't really necessary).

Here's some code I found with google:

C#
public static class ExtensionMethods
{
    public static IEnumerable<T[]> Permute<T>(this T[] xs, params T[] pre)
    {
        if (xs.Length == 0)
        {
            yield return pre;
        }
        for (int i = 0; i < xs.Length; i++)
        {
            foreach (T[] y in Permute(xs.Take(i).Union(xs.Skip(i+1)).ToArray(), pre.Union(new[] { xs[i] }).ToArray()))
            {
                yield return y;
            }
        }
    }
}


And here's how to use it:

C#
int[] values = {9,1,3,9};
HashSet<int[]> permutations = new HashSet<int[]>();
foreach(int[] perm in values.Permute())
{
    permutations.Add(perm);
}


This will yield 16 permutations. If you change the last digit to an 8, you'll get 24 permutations.
 
Share this answer
 
Comments
Amir Mahfoozi 17-Dec-11 10:42am    
+5 good answer.
Sergey Alexandrovich Kryukov 18-Dec-11 18:42pm    
My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900