How to Get All Combinations of a Certain Character Set

Posted on : 17 Feb, 10:00 PM


A friend of mine recently asked me a question that was challenging to answer. The question was, “What’s the easiest way to write a .Net code (algorithm) that generates all possible combinations of 3 characters together, considering the valid characters are from A to Z or any symbols?”

Before we go any further, you may be wondering why you’d need such an algorithm in real life. A real example would be Serial Number Generation. That is, generating serial numbers for spare parts. Usually, such serial numbers have certain patterns and you don’t want to have gaps between them. For example, when IBM produces laptops, each laptop has a serial number from a certain possible character set. An algorithm can be used to generate these serial numbers.

Another scenario that utilizes algorithms is when you need to break a password. Say you know the character set that might be used on the password, and you know the minimum and maximum length of it. From there, all you need to do is start trying all possible combinations of possible characters until you break it. An algorithm can do this for you.

Searching for an answer to her question, my friend had already thought about writing 3 nested for loops. Well, that’s actually the easiest way, but it’s also the hardest to maintain. What if we need to make it the combination of 5 characters instead of 3 characters? She suggested using 5 nested loops and so on. However, I didn’t like solving this issue with nested for loops.

So I thought of a better way. My idea was derived from numeral systems. For example, each digit of the decimal numeral system (base ten) might have values between 0 and 9. This means each digit only has 10 possibilities. Thus, if I invented a new numeral system in which any digit might have 26 possibilities (0 – 25), and then used this numeral system as an index for an array that contains all possible character sets, this could be a good solution.

My approach depends on adding all valid possible characters to an array of strings with a single for loop statement. This loop statement should go over a portion of code by the maximum number of combinations. The expected output is to generate a single combination from the character sets that are already defined as an array of strings.

Below is code snap that demonstrates how we can do that: [sourcecode language=’c#’] static void Main(string[] args) { string[] validCharSet = new string[] {“a”,”b”,”c”,”d”,”e”,”f” }; /* add all valid character set./ int combinationLength = 3; int[] indexes = new int[combinationLength]; /indexes for validCharSet array/ int maxPossibilities =(int) Math.Pow(validCharSet.Length, combinationLength); / calculate the max possible combinations./ int incrementBy = 0; / the value of incrementing the next digit./ /generate all combinations…*/ for (int i = 0; i < maxPossibilities; i++) {

//Output the valid combination of Character Set./ for (int l = 0; l < combinationLength; l++) Console.Write(validCharSet[indexes[l]]); Console.WriteLine(); incrementBy = 1; for (int j = indexes.Length-1; j >= 0; j–) { /if the digit reach to the Max allowed value, the reset it and increment the next digit./ if (indexes[j] >= validCharSet.Length) { indexes[j] = 0;

incrementBy = 1; } else { incrementBy = 0; } } } Console.ReadKey(); } [/sourcecode]

Related Posts

No related posts found
;

How to Get All Combinations of a Certain Character Set

Posted on : 17 Feb, 10:00 PM


A friend of mine recently asked me a question that was challenging to answer. The question was, “What’s the easiest way to write a .Net code (algorithm) that generates all possible combinations of 3 characters together, considering the valid characters are from A to Z or any symbols?”

Before we go any further, you may be wondering why you’d need such an algorithm in real life. A real example would be Serial Number Generation. That is, generating serial numbers for spare parts. Usually, such serial numbers have certain patterns and you don’t want to have gaps between them. For example, when IBM produces laptops, each laptop has a serial number from a certain possible character set. An algorithm can be used to generate these serial numbers.

Another scenario that utilizes algorithms is when you need to break a password. Say you know the character set that might be used on the password, and you know the minimum and maximum length of it. From there, all you need to do is start trying all possible combinations of possible characters until you break it. An algorithm can do this for you.

Searching for an answer to her question, my friend had already thought about writing 3 nested for loops. Well, that’s actually the easiest way, but it’s also the hardest to maintain. What if we need to make it the combination of 5 characters instead of 3 characters? She suggested using 5 nested loops and so on. However, I didn’t like solving this issue with nested for loops.

So I thought of a better way. My idea was derived from numeral systems. For example, each digit of the decimal numeral system (base ten) might have values between 0 and 9. This means each digit only has 10 possibilities. Thus, if I invented a new numeral system in which any digit might have 26 possibilities (0 – 25), and then used this numeral system as an index for an array that contains all possible character sets, this could be a good solution.

My approach depends on adding all valid possible characters to an array of strings with a single for loop statement. This loop statement should go over a portion of code by the maximum number of combinations. The expected output is to generate a single combination from the character sets that are already defined as an array of strings.

Below is code snap that demonstrates how we can do that: [sourcecode language=’c#’] static void Main(string[] args) { string[] validCharSet = new string[] {“a”,”b”,”c”,”d”,”e”,”f” }; /* add all valid character set./ int combinationLength = 3; int[] indexes = new int[combinationLength]; /indexes for validCharSet array/ int maxPossibilities =(int) Math.Pow(validCharSet.Length, combinationLength); / calculate the max possible combinations./ int incrementBy = 0; / the value of incrementing the next digit./ /generate all combinations…*/ for (int i = 0; i < maxPossibilities; i++) {

//Output the valid combination of Character Set./ for (int l = 0; l < combinationLength; l++) Console.Write(validCharSet[indexes[l]]); Console.WriteLine(); incrementBy = 1; for (int j = indexes.Length-1; j >= 0; j–) { /if the digit reach to the Max allowed value, the reset it and increment the next digit./ if (indexes[j] >= validCharSet.Length) { indexes[j] = 0;

incrementBy = 1; } else { incrementBy = 0; } } } Console.ReadKey(); } [/sourcecode]

Related Posts

No related posts found
;

footer-img

Integrant’s Vision is to transform the software development lifecycle through predictable results.

Subscribe

To get our newsletter & stay updated

© 2023 Integrant, Inc. All Rights Reserved | Privacy