I need to create a set A={“a”, “b”, “c”, “d” “e”} and print all the permutations of the set with length 3 that satisfy the condition: element "a" has to be ahead of "c" and "d" has to occur at least once.
Asked
Active
Viewed 401 times
0
-
Gorjan, I am having second thought about my answer. Do you mean that "a" and "c" have to always be present in the result, or that if they are both present, then "a" should come before "c"? – MarcoB May 20 '15 at 18:24
-
im really not sure – Gorjan May 20 '15 at 18:29
-
Hmm. If you don't know what problem you want to solve, then it's quite hard to solve it... – MarcoB May 20 '15 at 18:31
-
i think the result should be {a,c,d}{d,a,c}{a,d,c} – Gorjan May 20 '15 at 18:36
1 Answers
1
Select[Tuples[{"a", "b", "c", "d", "e"}, 3],
Count[#, "d"] == 1 && MatchQ[#, {___, "c", ___, "a"}] &]
(*
{{"c", "d", "a"}, {"d", "c", "a"}}
*)
David G. Stork
- 41,180
- 3
- 34
- 96