Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60162 > unrolled thread

Re: Recursive generator for combinations of a multiset?

Started byOscar Benjamin <oscar.j.benjamin@gmail.com>
First post2013-11-21 11:42 +0000
Last post2013-11-21 11:42 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Recursive generator for combinations of a multiset? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-11-21 11:42 +0000

#60162 — Re: Recursive generator for combinations of a multiset?

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-11-21 11:42 +0000
SubjectRe: Recursive generator for combinations of a multiset?
Message-ID<mailman.3007.1385034193.18130.python-list@python.org>
On 21 November 2013 06:46, John O'Hagan <research@johnohagan.com> wrote:
>
> I found a verbal description of such an algorithm and came up with
> this:
>
> def multicombs(it, r):
>     result = it[:r]
>     yield result
>     while 1:
>         for i in range(-1, -r - 1, -1):
>             rep = result[i]
>             if rep < it[i]:
>                 break
>         else:
>             break
>         for j, n in enumerate(it):
>             if n > rep:
>                 break
>         result = result[:i] + it[j:j - i]
>         yield result

I'm not really sure what it is you're asking for. I thought if I ran
the code I'd understand but that just confused me more. Is the output
below correct? If not what should it be?

multicombs("abracadabra", 0)
['']
multicombs("abracadabra", 1)
['a']
multicombs("abracadabra", 2)
['ab', 'br', 'ra']
multicombs("abracadabra", 3)
['abr', 'ara', 'bra']
multicombs("abracadabra", 4)
['abra']
multicombs("abracadabra", 5)
['abrac', 'abrbr', 'abrra', 'braca', 'brara', 'brbra', 'racad',
'racbr', 'racra']


Oscar

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web