Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate character combinations based on arbitrary string and length - similar to permutations
    primarykey
    data
    text
    <p>This question was asked before in other languages but not delphi after searching SO. see this question:<a href="https://stackoverflow.com/questions/17934622/how-to-generate-permutations-with-repeated-characters">How to Generate Permutations With Repeated Characters</a> and this question: <a href="https://stackoverflow.com/questions/2380962/generate-all-combinations-of-arbitrary-alphabet-up-to-arbitrary-length">Generate all combinations of arbitrary alphabet up to arbitrary length</a> and this one: <a href="https://stackoverflow.com/questions/18772706/how-to-generate-combination-of-fix-length-strings-using-a-set-of-characters">How to generate combination of fix length strings using a set of characters?</a> so the question is not new but I am having a hard time translating any of this to delphi. </p> <p>What I'm trying to do is generate combinations that does include repeats of characters such as this: if we have a string of characters (specified by user): ABC and we want to generate length of three characters (also length specified by user) I would get: <code>AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC etc...</code></p> <p>This code seems to do this but in C++:</p> <pre class="lang-cpp prettyprint-override"><code>int N_LETTERS = 4; char alphabet[] = {'a', 'b', 'c', 'd'}; std::vector&lt;std::string&gt; get_all_words(int length) { std::vector&lt;int&gt; index(length, 0); std::vector&lt;std::string&gt; words; while(true) { std::string word(length); for (int i = 0; i &lt; length; ++i) word[i] = alphabet[index[i]]; words.push_back(word); for (int i = length-1; ; --i) { if (i &lt; 0) return words; index[i]++; if (index[i] == N_LETTERS) index[i] = 0; else break; } } } </code></pre> <p>This also seems to do this:</p> <pre class="lang-cpp prettyprint-override"><code> #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;math.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; using namespace std; void displayPermutation(string permutation[], int length){ int i; for (i=0;i&lt;length;i++){ cout&lt;&lt;permutation[i]; } cout &lt;&lt; endl; } void getPermutations(string operatorBank[], int operatorCount, string permutation[],int permutationLength, int curIndex){ int i; //stop recursion condition if(curIndex == permutationLength){ displayPermutation(permutation,permutationLength); } else{ for(i = 0; i &lt; operatorCount; i++){ permutation[curIndex] = operatorBank[i]; getPermutations(operatorBank,operatorCount,permutation, permutationLength,curIndex+1); } } } int main () { int operatorCount = 4; int permutationLength = 3; string operatorBank[] = {"+","-","*","/"}; string permutation[] = {"","","",""}; //empty string int curIndex = 0; getPermutations(operatorBank,operatorCount,permutation, permutationLength,curIndex); return 0; } </code></pre> <p>closest to what I want in delphi is found here but does not allow <code>AAA</code> for example: <a href="http://www.swissdelphicenter.ch/torry/showcode.php?id=1032" rel="nofollow noreferrer">http://www.swissdelphicenter.ch/torry/showcode.php?id=1032</a></p> <p>And no this is not homework in case you are guessing. No other motive but just learning.</p> <p><strong>EDIT3:</strong> Removed all irrelevant code from question to make it easier for other people to read it and get to the answers below. Look under answers for 2 different methods to accomplish this: one using recursion and the other by using a counter function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload