c# - How can I get all subsets of a set that respect the order -
i'm looking c# example give me subsets of set while respecting order.
for example have a,b , like:
a b ab ba
please note purpose ab != ba
.
the solution should generic input type:
list<list<t>> getsubsets<t>(list<t> originalset)
i've come across great solutions using bitwise manipulation ab == ba (e.g generate combinations list of strings) far have failed find solve described above.
any tips/pointers appreciated!
getpermutations() ref. https://stackoverflow.com/a/10630026/1287352
public static list<list<t>> permutationof<t>(hashset<t> set) { var result = new list<list<t>>(); (var length = 1; length <= set.count; length++) { result.addrange(getpermutations<t>(set, length).select(i => i.tolist())); } return result; } private static ienumerable<ienumerable<t>> getpermutations<t>(ienumerable<t> list, int length) { if (length == 1) return list.select(t => new t[] { t }); return getpermutations(list, length - 1) .selectmany(t => list.where(e => !t.contains(e)), (t1, t2) => t1.concat(new t[] { t2 })); }
usage:
permutationof(new hashset<guid>() { guid.newguid(), guid.newguid(), guid.newguid() }) permutationof(new hashset<string>() { "a", "b", "c" })
result:
a b c a, b a, c b, b, c c, c, b a, b, c a, c, b b, a, c b, c, c, a, b c, b,
Comments
Post a Comment