Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60588
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <johnmohagan@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'interpreter': 0.05; 'output': 0.05; '(so': 0.07; 'pypy': 0.07; 'remaining': 0.07; 'result,': 0.07; 'string': 0.09; 'okay': 0.09; 'spaces': 0.09; 'subset': 0.09; 'target,': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'missed': 0.12; "'':": 0.16; "'ate',": 0.16; "'be',": 0.16; '0),': 0.16; '1):': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'champion': 0.16; 'count,': 0.16; 'dict': 0.16; 'direct,': 0.16; 'grouped': 0.16; 'ignoring': 0.16; 'iterates': 0.16; 'length,': 0.16; 'length.': 0.16; 'pruning': 0.16; 'pypy.': 0.16; 'redundancy': 0.16; 'redundant': 0.16; 'simplified': 0.16; 'subject:combinations': 0.16; 'subject:generator': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'file,': 0.19; 'version.': 0.19; 'seems': 0.21; 'input': 0.22; 'example': 0.22; '+0000': 0.22; 'cc:addr:python.org': 0.22; 'skip:e 30': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'long,': 0.26; 'this:': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'words': 0.29; "i'm": 0.30; 'code': 0.31; '(although': 0.31; 'produces': 0.31; 'lists': 0.32; 'checked': 0.32; 'quite': 0.32; 'call.': 0.33; 'checking': 0.33; 'totally': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'possible.': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'words,': 0.36; 'yield': 0.36; 'doing': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'changing': 0.37; 'example,': 0.37; 'list': 0.37; 'being': 0.38; 'nov': 0.38; 'needed': 0.38; 'fact': 0.38; 'rather': 0.38; 'explain': 0.39; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'letters': 0.60; 'length': 0.61; 'lower': 0.61; 'new': 0.61; 'john': 0.61; 'further': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'effectively': 0.66; 'worth': 0.66; 'results': 0.69; 'inline': 0.74; '100%': 0.77; 'avoids': 0.84; 'characters,': 0.84; 'is!': 0.84; 'oscar': 0.84; 'quickest': 0.84; 'recursion,': 0.84; 'words)': 0.84; 'to:none': 0.92; '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:cc:subject:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=8HRvygYvRhhtisAr9uEq8p+qyO1vB5vxtV4fGADYglI=; b=LgUvDoUZL/X9AiFl6kfoHViTR9ZoNktrc/9iodNvb0OzPSTa2+RhaO8cYVflFPdxuZ Y87ZXGJHxuPlPLHAHtxJJCEwLNuz1jqAiE6ojtGt5aQssQMnt74cZHdWD2/F/ksqGVPr Fg+yYx+eH/KOSOf9BjpK4UwX86inXxeVct5y97Czrf90QOM5kTFKxGulZV0iIBoLhmhq A8IzjiJybYWZ5ws0J5FqTMiYnczOn2D4SMBVif5yUHy/dbzaN8ESF1oRHHzm4GB1Au20 1QCd563D2XMAXhOiFZqvpoVsViZrJQKOGAhMpCG6z/hdos9pa/CzChe7GfWvVTh3ICyG DjTA== |
| X-Received | by 10.68.236.133 with SMTP id uu5mr4261616pbc.153.1385547921345; Wed, 27 Nov 2013 02:25:21 -0800 (PST) |
| Sender | "John O'Hagan" <johnmohagan@gmail.com> |
| Date | Wed, 27 Nov 2013 21:25:12 +1100 |
| From | John O'Hagan <research@johnohagan.com> |
| Cc | Python List <python-list@python.org> |
| Subject | Re: Recursive generator for combinations of a multiset? |
| In-Reply-To | <CAHVvXxS9NCzD_DEOT_t9Hg=SKC_FZ4AyeXfOV18SygCT=KmsPQ@mail.gmail.com> |
| References | <20131121174614.53450d51@mini.home> <CAHVvXxQ0Kdd91nCmmz6fw0fMiA=1nrPT=DLZAxhrFNhsuS89DA@mail.gmail.com> <20131122000115.3eb9e560@mini.home> <CAHVvXxT=ZSrLiuRGZP_UFZbgQatzfKUpGCHyzA=vVDtDc0BDrg@mail.gmail.com> <20131126171841.521bf1c8@mini.home> <CAHVvXxS9NCzD_DEOT_t9Hg=SKC_FZ4AyeXfOV18SygCT=KmsPQ@mail.gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3286.1385547929.18130.python-list@python.org> (permalink) |
| Lines | 139 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385547929 news.xs4all.nl 15950 [2001:888:2000:d::a6]:60517 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60588 |
Show key headers only | View raw
On Tue, 26 Nov 2013 10:33:06 +0000
Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote:
> On 26 November 2013 06:18, John O'Hagan <research@johnohagan.com>
> wrote:
> >
[...]
> >
> > def _multicombs(prepend, words, r, chkstr):
> > """chkstr is the string of remaining availalable characters"""
> > if r == 0:
> > yield prepend, chkstr
> > return
> > (word, count, rem), *remaining = words
> > newstr = chkstr
> > for k in range(max(r-rem, 0), min(count, r) + 1):
> > for letter in word * k:
> > if letter in newstr:
> > newstr = newstr.replace(letter, '' , 1)
> > else:
> > return
> > yield from _multicombs(prepend + (word,) * k, remaining,
> > r-k, newstr)
>
> Further micro-optimisations are possible. One is to inline the lower
> recursion levels. For example if the termination condition is checked
> immediately before recursion you can eliminate a redundant generator
> function call.
>
> > By progressively checking against the available characters, it
> > avoids fruitless recursion branches. I'm not 100% sure I'm doing
> > this right yet but so far it's promising. This is the quickest
> > non-redundant approach I've used so far (although strangely, the
> > original product-only version which produces a lot of redundant
> > results is still by far the quickest.)
>
> Bear in mind that the interpreter does a lot of "redundant" things so
> what you might think of as non-redundant code can actually be highly
> redundant when interpreter over-head is accounted for.
I don't mind redundancy as long as I don't have to see it in the output!
> Recently I've been trying out PyPY in my own work and it is *really
> fast*. In fact it is so much faster that I've given up on the idea of
> speed-optimising code for CPython: just use PyPy instead - although it
> is worth optimising highly intensive code for PyPy.
I'll have a look at it.
> > I'll try to explain better what I'm actually doing. It's quite long,
> > but you seem to be wondering, or maybe someone else is!
>
> [snip]
>
> Okay I understand now. I was confused because I think of anagrams as
> being words of the same length. I didn't understand how your multiword
> version works but I think I do now. In my own words: You want to
> generate all sequences of English words having the same letters as
> some input string, ignoring the spaces in both the input and output
> strings (so that the number or length of the individual words need not
> be the same).
>
> [snip]
> >
> > For example, take the phrase "break beat". I make a wordlength
> > dictionary of sub-alphabet words, using the system dictionary file,
> > like this:
> >
> > {1: ['a'], 2: ['at', 'be', 're'], 3: ['are', 'ark', 'art', 'ate',
> > 'baa', 'bar', 'bat', 'bee', 'bet', 'bra', 'ear', 'eat', 'ebb',
> > 'eke', 'era', 'ere', 'eta', 'rat', 'tab', 'tar', 'tea', 'tee'], 4:
> > ['abet', 'area', 'babe', 'bake', 'barb', 'bare', 'bark', 'bate',
> > 'beak', 'bear', 'beat', 'beer', 'beet', 'beta', 'brat', 'rake',
> > 'rate', 'reek', 'take', 'tare', 'teak', 'tear', 'tree', 'trek'], 5:
> > ['abate', 'baker', 'beret', 'brake', 'break', 'eater', 'karat',
> > 'kebab', 'taker'], 6: ['aerate', 'beaker', 'beater', 'berate',
> > 'betake', 'karate', 'rebate', 'retake']}
>
> Okay, how about the following (pseudoish-code)?
>
> def word_sequences(prepend, target, subwords):
> # subwords is a list rather than a dict
> for n in range(1, len(subwords)):
> for word in subwords[n]:
> if equal_in_a_multiset_sense(word, target):
> yield prepend + ' ' + target
> elif is_a_submultiset(word, target):
> recurse_target = multiset_subtract(target, word)
> subsubwords =
> list_of_subwords_by_length[:len(recurse_target)] if any(subsubwords):
> yield from word_sequences(prepend + ' ' + word,
> recurse_target, subsubwords)
>
[...]
That is very clever. Direct, economical and does effectively the
same pruning job as my approach without all the combinatorial
brainstrain. I got it working by changing "for n in range..." to "for
words in subwords" (otherwise it missed the one-letter words) and the
first yield needed to be "prepend + ' ' + word".
I simplified it a bit more to this:
def word_sequences(prepend, target, subwords):
"""subwords is a list of lists of subwords grouped by length,
in order of length"""
for words in subwords:
for word in words:
recurse_target = subset_subtract(target, word)
if recurse_target:
yield from word_sequences(prepend + ' ' + word,
recurse_target, subwords[:len(recurse_target)])
elif recurse_target == '':
yield prepend + ' ' + word
with just one function to do the subset testing:
def subset_subtract(target, word):
for i in word:
if i in target:
target = target.replace(i, '' ,1)
else:
return
return target
Speed-wise it is somewhat faster than any of my non-duplicate-producing
attempts, but still way slower than the current champion, the redundant
cartesian product-only version.
However, because it iterates through all the remaining words on each
recursion, it seems to produce n! of each unique result, where n in the
number of words in the result, so this is the new champion as far as
redundancy is concerned. I'll keep working on it, the totally
different approach is interesting.
--
John
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Recursive generator for combinations of a multiset? John O'Hagan <research@johnohagan.com> - 2013-11-27 21:25 +1100
csiph-web