Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Method to generate all permuatuons of spinnable strings</strong></p> <p>I've implemented a simple method to solve this problem. It takes an ArrayList argument containing spinnable text string(s). I use it to generate all the permutations of multiple spinnable strings.</p> <p>It comes with extra functionality of support of optional blocks, surronded by "[ ]" brackets.</p> <p>Eq.: If you have a single string object in the ArrayList with content of: {A | {B1 | B2 } [B optional] }</p> <p>It populates the array list with all the permutations, "extracted" Contents after invocation of method: A B1 B1 B optional B2 B2 B optional</p> <p>You can also pass multiple strings as argument to generate permutations for all of them: Eg.: Input: ArraList with two string {A1 | A2} {B1 | B2} Contents after invocation: A1 A2 B1 B2</p> <p>This implementation works by always finding the inner most bracket pair in the first spinnable section, then extract it. I do this until all the special {}, [] characters are removed.</p> <pre><code>private void ExtractVersions(ArrayList list) { ArrayList IndicesToRemove = new ArrayList(); for (int i = 0; i &lt; list.Count; i++) { string s = list[i].ToString(); int firstIndexOfCurlyClosing = s.IndexOf('}'); int firstIndexOfBracketClosing = s.IndexOf(']'); if ((firstIndexOfCurlyClosing &gt; -1) || (firstIndexOfBracketClosing &gt; -1)) { char type = ' '; int endi = -1; int starti = -1; if ((firstIndexOfBracketClosing == -1) &amp;&amp; (firstIndexOfCurlyClosing &gt; -1)) { // Only Curly endi = firstIndexOfCurlyClosing; type = '{'; } else { if ((firstIndexOfBracketClosing &gt; -1) &amp;&amp; (firstIndexOfCurlyClosing == -1)) { // Only bracket endi = firstIndexOfBracketClosing; type = '['; } else { // Both endi = Math.Min(firstIndexOfBracketClosing, firstIndexOfCurlyClosing); type = s[endi]; if (type == ']') { type = '['; } else { type = '{'; } } } starti = s.Substring(0, endi).LastIndexOf(type); if (starti == -1) { throw new Exception("Brackets are not valid."); } // start index, end index and type found. -&gt; make changes if (type == '[') { // Add two new lines, one with the optional part, one without it list.Add(s.Remove(starti, endi - starti+1)); list.Add(s.Remove(starti, 1).Remove(endi-1, 1)); IndicesToRemove.Add(i); } else if (type == '{') { // Add as many new lines as many alternatives there are. This must be an in most bracket. string alternatives = s.Substring(starti + 1, endi - starti - 1); foreach(string alt in alternatives.Split('|')) { list.Add(s.Remove(starti,endi-starti+1).Insert(starti,alt)); } IndicesToRemove.Add(i); } } // End of if( &gt;-1 &amp;&amp; &gt;-1) } // End of for loop for (int i = IndicesToRemove.Count-1; i &gt;= 0; i--) { list.RemoveAt((int)IndicesToRemove[i]); } } </code></pre> <p>I hope I've helped. Maybe it is not the simplest and best implementation, but it works well for me. Please feedback, and vote!</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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