Combinations Of String While Maintaining Order Of Words

前端 未结 3 1562
庸人自扰
庸人自扰 2021-01-24 20:17

Given a string:

String words = \"Mary had a little lamb\";

how to obtain a combination of sentence fragments while the order of occurrence of w

相关标签:
3条回答
  • 2021-01-24 20:43

    You have 4 word delimiters (spaces) here. Every space may be replaced (or not) by sentence delimiter. So there are 16 = 2^4 cases, which correspond to binary numbers 0000...1111.

    0 讨论(0)
  • 2021-01-24 20:45

    To get the output shown in your question, though not in the same order, this is what I would do.
    I will be using Mathematica code, but the concepts are universal.

    string = "Mary had a little lamb";
    set = StringSplit[string]
    n = Length@set
    
    {"Mary", "had", "a", "little", "lamb"}
    5
    

    So you will need a function that breaks the sentence into words (StringSplit).

    Then you will need a function to generate integer partitions and a permutation function that is aware of duplicate elements. Algorithms for both can be found here on StackOverflow.

    IntegerPartitions[n]
    
    {{5}, {4, 1}, {3, 2}, {3, 1, 1}, {2, 2, 1}, {2, 1, 1, 1}, {1, 1, 1, 1, 1}}
    

    Once we permute each partition ("for each" is /@) we get all ways to linearly split a set of five parts:

    parts = Join @@ Permutations /@ IntegerPartitions[n]
    
    {{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}}
    

    Finally we need a function to split a set according to a sequences of lengths. I call mine dynamicPartition:

    dynamicPartition[set, #] & /@ parts // Column
    
    {{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}}
    
    0 讨论(0)
  • 2021-01-24 20:49

    Think about it this way:

    Mary <1> had <2> a <3> little <4> lamb
    

    Each of these <number>s can be either true or false. If it is true, then you cut the sentence in that location.

    So, if you have n+1 words, your problem gets reduced to going through binary representation of numbers with n bit, that is from 0 to 2^n-1

    Examples:

    0110 -> {'Mary had', 'a', 'little lamb'}
    1111 -> {'Mary', 'had', 'a', 'little', 'lamb'}
    0001 -> {'Mary had a little', 'lamb'}
    1011 -> {'Mary', 'had a', 'little', 'lamb'}
    
    0 讨论(0)
提交回复
热议问题