Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'argument': 0.05; 'output': 0.05; 'advice.': 0.09; 'def': 0.12; "'in',": 0.16; "'the',": 0.16; '-1):': 0.16; 'benjamin': 0.16; 'combinations': 0.16; 'pruning': 0.16; 'redundant': 0.16; 'shortcut': 0.16; 'subject:combinations': 0.16; 'subject:generator': 0.16; 'to:name:python list': 0.16; 'tuple': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'thu,': 0.19; 'verbal': 0.19; '+0000': 0.22; 'skip:l 30': 0.24; 'mention': 0.26; 'this:': 0.26; 'asking': 0.27; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'words': 0.29; "doesn't": 0.30; 'said,': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'post.': 0.31; 'produces': 0.31; 'actual': 0.34; "i'd": 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'combination': 0.36; 'otherwise.': 0.36; 'words,': 0.36; 'yield': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'nov': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'above,': 0.60; 'algorithms': 0.60; 'experts': 0.60; 'ground': 0.60; 'break': 0.61; 'length': 0.61; 'john': 0.61; "you're": 0.61; 'first': 0.61; 'offer': 0.62; 'such': 0.63; 'skip:n 10': 0.64; 'results': 0.69; 'guaranteed': 0.75; 'hoping': 0.75; 'oscar': 0.84; 'forgotten': 0.91; 'received:110': 0.96; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:subject:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=mvlL481+QN49BbxcYlntjbK3iGYYiG3ROsQfAQgJDoo=; b=XqsF5DL5HmvzUY/RTrUIuXFA5Aqbz8aQyveKUoaDLLaROU6X+pIl10Dwz+W4Jbj1Bb o+e09X+w2odAyESFNkmimYOuNz2uQLxFUs4WKih3KncwixEV0JqcVBlnlzmlC+N/IkAT BnsUd6kFC4x2f3oXxVEE1Ny950FSDyllxolFJLCGKlC0ylqWCeFPfeCvsmHHfZwIvu81 apXNcYjF3Unyq6ZgCY/VSCt10k6jR7vA8XGl7otGzdjqriUFfRNpsTu/Ems0yIG8QBHo xsFG+7aBFvODgWkhmEJ4Q0gedKImc/uioYFhPgqjy/4W9u7/Fa1JYq+0LNyENVEv4ANv FxPQ== X-Received: by 10.66.121.234 with SMTP id ln10mr6125213pab.20.1385038882016; Thu, 21 Nov 2013 05:01:22 -0800 (PST) Sender: "John O'Hagan" Date: Fri, 22 Nov 2013 00:01:15 +1100 From: John O'Hagan To: Python List Subject: Re: Recursive generator for combinations of a multiset? In-Reply-To: References: <20131121174614.53450d51@mini.home> X-Mailer: Claws Mail 3.9.2 (GTK+ 2.24.22; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385038886 news.xs4all.nl 15970 [2001:888:2000:d::a6]:60846 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60163 On Thu, 21 Nov 2013 11:42:49 +0000 Oscar Benjamin wrote: > On 21 November 2013 06:46, John O'Hagan > 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'] I neglected to mention that multicombs takes a sorted iterable; it doesn't work right otherwise. I'd forgotten that because my wordlists are guaranteed sorted by the way they're built. Sorry about that. In my use-case the first argument to multicombs is a tuple of words which may contain duplicates, and it produces all unique combinations of a certain length of those words, eg: list(multicombs(('cat', 'hat', 'in', 'the', 'the'), 3)) [('cat', 'hat', 'in'), ('cat', 'hat', 'the'), ('cat', 'in', 'the'), ('cat', 'the', 'the'), ('hat', 'in', 'the'), ('hat', 'the', 'the'), ('in', 'the', 'the')] Contrast this with: list(itertools.combinations(('cat', 'hat', 'in', 'the', 'the'), 3)) [('cat', 'hat', 'in'), ('cat', 'hat', 'the'), ('cat', 'hat', 'the'), ('cat', 'in', 'the'), ('cat', 'in', 'the'), ('cat', 'the', 'the'), ('hat', 'in', 'the'), ('hat', 'in', 'the'), ('hat', 'the', 'the'), ('in', 'the', 'the')] which produces results which are redundant for my purposes. What I'm looking for is a recursive algorithm which does what multicombs does (order unimportant) so that I can apply a pruning shortcut like the one I used in the recursive cartesian product algorithm in my original post. Multiset combination algorithms seem pretty thin on the ground out there - as I said, I could only find a description of the procedure above, no actual code. The ones I did find are non-recursive. I'm hoping some combinatorics and/or recursion experts can offer advice. Regards, -- John