4
$\begingroup$

Given a list of letters,

letters = { "A", "B", ..., "F" }

is it possible to get Mathematica to generate all ‘words’ (in this example, 6 letter words), if only one letter can be used one time only, e.g. ABCDEF, ABCDFE, …? TIA.

$\endgroup$
9
$\begingroup$

Pemutations will do it:

letters = {"a", "b", "c"};
Permutations[letters, {3}]
{{"a", "b", "c"}, {"a", "c", "b"}, {"b", "a", "c"}, 
 {"b", "c", "a"}, {"c", "a", "b"}, {"c", "b", "a"}}

To get all six-letter words:

letters = {"a", "b", "c", "d", "e", "f"};
perms = Permutations[letters, {6}];
StringJoin /@ perms

{"abcdef", "abcdfe", "abcedf", "abcefd", "abcfde" ... etc.

there are a lot of them.

$\endgroup$
7
$\begingroup$

You can create permutations with all of the letters as strings with:

StringJoin /@ Permutations[letters]

If you want lists of the individual letters just use:

Permutations[letters]

Check the documentation of Permutations to learn about permutations with subsets of letters. If you want to use each letter more than once, look at the documentation for Tuples.

$\endgroup$
  • $\begingroup$ Thanks(x2). Is there some way to check how many words contain a ‘sub-word’, like ‘ab’ or even a set of ‘sub-words’ like ‘ab’ and ‘cd’? And is there any web page or text book that deals with combinatorics in Mathematica (on a more ‘basic’ level) that I could visit/buy and read? $\endgroup$ – mf67 Mar 24 at 2:54
3
$\begingroup$

If I follow the OP's question, I think they want the following:

letters = {"a", "b", "c"};
p = Permutations[letters, {#}] & /@ Range[Length[letters]];
(StringJoin[#] & /@ #) & /@ p

{{a, b, c}, {ab, ac, ba, bc, ca, cb}, {abc, acb, bac, bca, cab, cba}}
$\endgroup$
  • $\begingroup$ No, the OP requested all six letter words. $\endgroup$ – m_goldberg Mar 24 at 11:22
  • $\begingroup$ Your last line can be written more cleanly as Map@StringJoin/@p or Map[StringJoin, p, {2}]. $\endgroup$ – Doorknob Mar 24 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.