The ability to programmatically create and use string permutations is a basic software testing skill. A string arrangement is a reordering of a set of strings. For example, if your initial group consists of three strings (Apple, banana, cherry), there are six permutations:
{"Apple", "banana", "cherry"}
{"Apple", "cherry", "banana"}
{"Banana", "apple", "Cherry"}
{"Banana", "cherry", "Apple"}
{"Cherry", "apple", "banana"}
{"Cherry", "banana", "apple"}
In software testing, the typical use of permutations is to generate test case data for unit, API, and module tests. Suppose you have a method that accepts three strings, and a test case is entered as "apple," "Banana," "Cherry." In most cases, you want to create five other test cases that use other input permutations. In software testing, permutations have many other uses. In fact, alignment is so important and popular in software engineering that the issue of alignment becomes one of the most common types of interview questions for Microsoft testing work.
A good way to find out what I've said in this month's column is through a screenshot. Figure 1 shows the running of a demo program. (The complete source code for generating the demo is available for download in this column.) As the output in Figure 1 indicates, the three basic skills associated with string alignment are the ability to accurately compute the number of permutations for a given string group, the ability to generate all permutations, and the ability to generate a specific arrangement.
Figure 1 String Combination Demo
Because of the slightly different permutation terms between problem domains, let's build a few key terms that will be used throughout the column. The term permutation element refers to an ordered set of strings formed by an initial group. The term atom means an array of any single string in an element. The order of the permutations is the number of atoms in any element. I'll use an arrangement in the dictionary editing order. This means that the atoms of the initial permutation element (also known as the identity Element) are listed in a dictionary order. Each permutation element is also sorted relative to the other elements. This means that there will be a different identity arrangement element, a different last element, and that each element has a subsequent element and a forward element (except for the first element without a successor element and the last element). Finally, the term string arrangement refers only to an arrangement element whose atoms are strings. The reason I use this term is that the mathematical arrangement of order n has multiple integer atoms, from 0 to n-1 (for example, {0, 1, 2}). The total number of permutation elements for order n is n! (read as "n factorial"). For example, if there are four atoms, the total number of permutation elements is:
4! = 4 * 3 * 2 * 1 = 24
The reason should be easy to understand. For the first atom in an element, you can select any of the n atoms. For the second atom, you can select any of the remaining n-1 atoms. Wait a minute.
Permutations are closely related to combinations, but sometimes they are confused with combinations. A string combination is a subset of the initial string group, where the order is irrelevant. For example, suppose the initial string group consists of apple, banana, and cherry. The subset size k = 1 o'clock has three combination elements:
{"Apple"}, {"Banana"}, {"Cherry"}