Build all combinations of strings

bearophile bearophileHUGS at lycos.com
Mon Jun 11 12:52:37 PDT 2012


Andrej Mitrovic:

> string[] words = "foo bar doo".split();
>
> into:
>
> string[] res = ["foo bar doo",
>                 "foo doo bar",
>                 "bar foo doo",
>                 "bar doo foo",
>                 "doo foo bar",
>                 "doo bar foo"];

http://rosettacode.org/wiki/Permutations#Faster_Lazy_Version

Using that the code is:

import std.string, std.stdio, std.array;
void main() {
     auto words = "foo bar doo".split();
     auto res = permutations!false(words).map!(p => p.join(" 
"))().array();
     writeln(res);
}


The output is your desired one:

["foo bar doo", "foo doo bar", "bar foo doo", "bar doo foo", "doo 
foo bar", "doo bar foo"]

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list