Click here to Skip to main content
15,887,083 members
Home / Discussions / C#
   

C#

 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
Pete O'Hanlon22-Apr-12 19:10
mvePete O'Hanlon22-Apr-12 19:10 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
BobJanova22-Apr-12 22:33
BobJanova22-Apr-12 22:33 
GeneralRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
Lutosław23-Apr-12 11:48
Lutosław23-Apr-12 11:48 
GeneralRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
BobJanova24-Apr-12 0:10
BobJanova24-Apr-12 0:10 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
Dave Kreskowiak23-Apr-12 6:58
mveDave Kreskowiak23-Apr-12 6:58 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
jschell23-Apr-12 10:53
jschell23-Apr-12 10:53 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
Lutosław23-Apr-12 11:43
Lutosław23-Apr-12 11:43 
AnswerRe: Generate all possible combination A-Z0-9 with 16 length(Permutations) Pin
Lutosław23-Apr-12 12:29
Lutosław23-Apr-12 12:29 
OK, seriously. I have a nasty feature: I like programming at 1 AM. Here is an enumerator which interates through all these combinations:
static void Main(string[] args)
{
    foreach (string sperm in new PermutationEnumerator(2))
        Console.WriteLine(sperm);
    Console.ReadLine();
}

public class PermutationEnumerator : IEnumerator<string>, IEnumerable<string>
{
    int stringLength;
    char[] table;
    char[] chars;
    bool go = true;

    public PermutationEnumerator(int stringLength)
    {
        chars = new char[36];
        this.stringLength = stringLength;
        for (char c = '0'; c <= '9'; c++)
            chars[c - '0'] = c;
        for (char c = 'A'; c <= 'Z'; c++)
            chars[c - 'A' + 10] = c;
        Reset();
    }
    public string Current
    {
        get
        {
            if (table == null || !go) return null;
            return new string(table); 
        }
    }
    bool PlusOne(ref char c)
    {
        bool overflow = false;
        if (c >= '0' && c < '9')
            c = (char)(c + 1);
        else if (c == '9')
            c = 'A';
        else if (c >= 'A' && c < 'Z')
            c = (char)(c + 1);
        else
        {
            c = '0'; 
            overflow = true;
        }
        return overflow;
    }
    public void Dispose()
    {                
    }

    object System.Collections.IEnumerator.Current
    {
        get { return Current; }
    }

    public bool MoveNext()
    {
        if (table == null)
        {
            Init();
            return table.Length > 0;
        }
        int i;
        for (i = table.Length - 1; i >= 0; i--)
        {
            if (!PlusOne(ref table[i]))
                break;
        }
        go = i >= 0;
        return go;
    }

    public void Reset()
    {
        table = null;
    }
    void Init()
    {
        table = new char[stringLength];
        for (int i = 0; i < stringLength; i++)
        {
            table[i] = chars[0];
        }
    }
    void AddOne()
    {
        for (int i = table.Length - 1; i >= 0; i--)
        {
            if (!PlusOne(ref table[i]))
                break;
        }
    }

    public IEnumerator<string> GetEnumerator()
    {
        return this;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Greetings - Jacek


modified 23-Apr-12 18:41pm.

Questionhandling ctrl+mouse click on webbrowser control in winform Pin
hoangminhtoan22-Apr-12 17:11
hoangminhtoan22-Apr-12 17:11 
AnswerRe: handling ctrl+mouse click on webbrowser control in winform Pin
VJ Reddy22-Apr-12 20:06
VJ Reddy22-Apr-12 20:06 
GeneralRe: handling ctrl+mouse click on webbrowser control in winform Pin
hoangminhtoan22-Apr-12 22:11
hoangminhtoan22-Apr-12 22:11 
GeneralRe: handling ctrl+mouse click on webbrowser control in winform Pin
VJ Reddy22-Apr-12 22:58
VJ Reddy22-Apr-12 22:58 
GeneralRe: handling ctrl+mouse click on webbrowser control in winform Pin
hoangminhtoan22-Apr-12 23:12
hoangminhtoan22-Apr-12 23:12 
QuestionAny free GS-1 Databar classes? Pin
nickslick22-Apr-12 10:17
nickslick22-Apr-12 10:17 
AnswerRe: Any free GS-1 Databar classes? Pin
DaveyM6922-Apr-12 11:49
professionalDaveyM6922-Apr-12 11:49 
GeneralRe: Any free GS-1 Databar classes? Pin
nickslick22-Apr-12 12:02
nickslick22-Apr-12 12:02 
Questionabout a code Pin
Gayani Udawatta22-Apr-12 4:26
Gayani Udawatta22-Apr-12 4:26 
AnswerRe: about a code Pin
Richard MacCutchan22-Apr-12 4:33
mveRichard MacCutchan22-Apr-12 4:33 
GeneralRe: about a code Pin
PIEBALDconsult22-Apr-12 6:46
mvePIEBALDconsult22-Apr-12 6:46 
GeneralRe: about a code Pin
Richard MacCutchan22-Apr-12 21:01
mveRichard MacCutchan22-Apr-12 21:01 
AnswerRe: about a code Pin
PIEBALDconsult23-Apr-12 3:14
mvePIEBALDconsult23-Apr-12 3:14 
GeneralRe: about a code Pin
Lutosław23-Apr-12 11:04
Lutosław23-Apr-12 11:04 
Questionconnect to access database Pin
MemberDotNetting22-Apr-12 3:39
MemberDotNetting22-Apr-12 3:39 
AnswerRe: connect to access database Pin
Wes Aday22-Apr-12 3:46
professionalWes Aday22-Apr-12 3:46 
GeneralRe: connect to access database Pin
MemberDotNetting22-Apr-12 3:48
MemberDotNetting22-Apr-12 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.