Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get the output shown in your question, though not in the same order, this is what I would do.<br> I will be using <em>Mathematica</em> code, but the concepts are universal.</p> <pre><code>string = "Mary had a little lamb"; set = StringSplit[string] n = Length@set </code></pre> <blockquote> <pre><code>{"Mary", "had", "a", "little", "lamb"} 5 </code></pre> </blockquote> <p>So you will need a function that breaks the sentence into words (StringSplit).</p> <p>Then you will need a function to generate <a href="http://en.wikipedia.org/wiki/Integer_partitions" rel="nofollow">integer partitions</a> and a permutation function that is aware of duplicate elements. Algorithms for both can be found here on StackOverflow.</p> <pre><code>IntegerPartitions[n] </code></pre> <blockquote> <pre><code>{{5}, {4, 1}, {3, 2}, {3, 1, 1}, {2, 2, 1}, {2, 1, 1, 1}, {1, 1, 1, 1, 1}} </code></pre> </blockquote> <p>Once we permute each partition ("for each" is <code>/@</code>) we get all ways to linearly split a set of five parts:</p> <pre><code>parts = Join @@ Permutations /@ IntegerPartitions[n] </code></pre> <blockquote> <pre><code>{{5}, {4, 1}, {1, 4}, {3, 2}, {2, 3}, {3, 1, 1}, {1, 3, 1}, {1, 1, 3}, {2, 2, 1}, {2, 1, 2}, {1, 2, 2}, {2, 1, 1, 1}, {1, 2, 1, 1}, {1, 1, 2, 1}, {1, 1, 1, 2}, {1, 1, 1, 1, 1}} </code></pre> </blockquote> <p>Finally we need a function to split a set according to a sequences of lengths. I call mine dynamicPartition:</p> <pre><code>dynamicPartition[set, #] &amp; /@ parts // Column </code></pre> <blockquote> <pre><code>{{Mary,had,a,little,lamb}} {{Mary,had,a,little},{lamb}} {{Mary},{had,a,little,lamb}} {{Mary,had,a},{little,lamb}} {{Mary,had},{a,little,lamb}} {{Mary,had,a},{little},{lamb}} {{Mary},{had,a,little},{lamb}} {{Mary},{had},{a,little,lamb}} {{Mary,had},{a,little},{lamb}} {{Mary,had},{a},{little,lamb}} {{Mary},{had,a},{little,lamb}} {{Mary,had},{a},{little},{lamb}} {{Mary},{had,a},{little},{lamb}} {{Mary},{had},{a,little},{lamb}} {{Mary},{had},{a},{little,lamb}} {{Mary},{had},{a},{little},{lamb}} </code></pre> </blockquote>
    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.
    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