From: "B. T. Raven" <nihil@nihilo.net>
To: help-gnu-emacs@gnu.org
Subject: Re: All Possible Combinations
Date: Wed, 03 Jun 2009 08:36:00 -0500 [thread overview]
Message-ID: <We2dneOpjcq54bvXnZ2dnUVZ_qCdnZ2d@sysmatrix.net> (raw)
In-Reply-To: <7chbyxprrn.fsf@pbourguignon.anevia.com>
Pascal J. Bourguignon wrote:
> Nordlöw <per.nordlow@gmail.com> writes:
>
>> Hey!
>>
>> I want a function that generates all possible combinations (ordering)
>> of the elements in a list (or sequence if possible). Here is my
>> mockup:
>>
>> (defun all-combinations (n)
>> "Generate a listing of all the possible combinations of the
>> elements in the sequence N. Time-Complexity is N!"
>> (let (all)
>> all))
>>
>> For example (all-combinations '(a b c)) should return '((a b c) (a c
>> b) (b a c) (b c a) (c a b) (c b a))
>>
>> Has somebody written such a function, preferrably in an iterative
>> rather than recursive way.
>
> It's called permutations. Combinations are when you take a smaller
> number of elements amongst the set.
Both permutations and combinations can deal with n things taken r at a
time where r can equal n:
nPr = n! / (n-r)!
nCr = n! / (n-r)! r!
So in general (at least when r /= 1) the number of resultant sets is
smaller with nCr
>
> Now notice that (permutations '()) = (())
> that is, the set of permutations of the empty set contains only the
> empty set (there's no other way to order no elements)
>
> and notice that (permutations '(a)) = ((a))
> (there's only one way to order one element).
>
> Now, knowing that (permutations '(a)) = ((a))
> How can you compute (permutations '(b a))?
> That is, how many ways can you put b in the permutations of (a), for example, in (a)?
>
> Yes, there's two ways: before or after a: (b a) or (a b).
>
>
> So now we know that (permutations '(b a)) = ((b a) (a b))
> Then, how can you compute (permutations '(c b a))?
> That is, how many ways can you put c in the permutations of (b a), for example, in (a b)?
>
> ...
>
> So now we know that (permutations '(x ...)) = ((x ...) ... (... x))
> Then, how can you compute (permutations '(y x ...))?
> That is, how many ways can you put y in the permutations of (x ...), for example, in (... x)?
>
>
next prev parent reply other threads:[~2009-06-03 13:36 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-03 9:09 All Possible Combinations Nordlöw
2009-06-03 9:53 ` Pascal J. Bourguignon
2009-06-03 13:36 ` B. T. Raven [this message]
2009-06-03 9:55 ` Lennart Borgman
2009-06-03 18:50 ` Marc Tfardy
2009-06-04 6:08 ` Nordlöw
2009-06-04 7:20 ` Marc Tfardy
2009-06-04 16:25 ` B. T. Raven
2009-06-05 15:49 ` Thierry Volpiatto
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=We2dneOpjcq54bvXnZ2dnUVZ_qCdnZ2d@sysmatrix.net \
--to=nihil@nihilo.net \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.