Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.04; 'suppose': 0.05; 'australian': 0.07; 'hettinger': 0.07; 'received :mail-qy0-f174.google.com': 0.07; 'python': 0.07; '>>>>': 0.09; 'cents': 0.09; 'satisfying': 0.09; 'url:activestate': 0.09; 'looked': 0.10; 'url:code': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '%s."': 0.16; 'operator.': 0.16; 'received:209.85.216.174': 0.16; 'recipes': 0.16; 'solver': 0.16; 'written,': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'code,': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'mon,': 0.22; 'raymond': 0.22; "i'm": 0.26; 'message- id:@mail.gmail.com': 0.28; '"the': 0.28; 'problem': 0.29; 'fri,': 0.29; 'least': 0.30; 'yet': 0.30; 'cc:addr:python.org': 0.31; 'confused': 0.31; 'spelling': 0.31; 'it.': 0.31; 'done': 0.32; '...': 0.32; 'change.': 0.33; 'words,': 0.33; 'with.': 0.33; 'using': 0.34; 'certain': 0.34; 'change': 0.34; 'there': 0.35; 'print': 0.35; 'put': 0.35; '-0700,': 0.35; 'visibility': 0.35; 'think': 0.36; 'some': 0.37; 'received:209.85': 0.37; '20,': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'cut': 0.39; 'word,': 0.39; 'received:209': 0.39; 'how': 0.39; 'similar': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'might': 0.40; 'dollar': 0.60; 'hand': 0.61; 'give': 0.61; '2011': 0.62; 'making': 0.62; 'worth': 0.64; 'play': 0.64; 'skip:5 10': 0.65; 'kinds': 0.67; 'fox': 0.73; 'with,': 0.73; 'instructive': 0.84; 'stupid': 0.84; 'cent': 0.91; 'technique': 0.93; 'brown': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=eIIPWnacVOwvyLMtu+UUE61VCMDFleEA1xELjZEmMmk=; b=le6mscj1rmlG+w/ZwNwjb3CY3vFJpCh2Z0x3GfCbkR9yCq8yOTkfWOEo80tkrBaqOe ktiyeZ5RqywjL8ceFgQJN7QJPJiLo6myXVExEZRpXagfztfyeQwCl66o9KfZUGIJ5ZFA hBnJGMVRqsMcPBz0QiNemdHY/jgjvLw5lR9Xk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=McZlkRtJPgXXfWVGWkm6n4kQYKgDRqYhK002hA/b1yvAa7BYEKfHuA6IYFGdmxVh1/ NVxur6npQm1cjAP0WNGnRtmUeR7JawJSixGRCZLd2wn+aeFrKjeUu2JCNN7Nv8vv3Rzm jiUGSaJvhkXPVHLdRNeBgyJSa6urSIKxSNAxM= MIME-Version: 1.0 In-Reply-To: <4dc428f2$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <69c1813d-1a9a-4686-9768-8ec1910a45f8@d19g2000prh.googlegroups.com> <4dc428f2$0$29991$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 6 May 2011 10:43:06 -0700 Subject: Re: Coolest Python recipe of all time From: geremy condra To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 73 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304703790 news.xs4all.nl 81473 [::ffff:82.94.164.166]:50235 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4842 On Fri, May 6, 2011 at 9:59 AM, Steven D'Aprano wrote: > On Mon, 02 May 2011 10:33:31 -0700, Raymond Hettinger wrote: > >> I think it is time to give some visibility to some of the instructive >> and very cool recipes in ActiveState's python cookbook. > [...] >> What are your favorites? > > > I'm not sure if favourite is the right word, but I'm amazed by this one: > McCarthy's "amb" (ambiguous) operator. > > http://code.activestate.com/recipes/577368 > > Here's how I might use it to solve the problem if making change. In > Australian currency, I have 5, 10, 20, 50 cent and $1 and $2 coins. > Ignoring the dollar coins, how can I make up change for any multiple of > five cents up to a dollar? > > Suppose I have more 5 cent coins that I can deal with, and I want to make > sure I hand out at least three of them. Here's how to make 45 cents worth > of change: > >>>> amb =3D Amb() >>>> a =3D amb(range(3, 21)) =A0# number of 5 cent coins >>>> b =3D amb(range(11)) =A0# number of 10 cent coins >>>> c =3D amb(range(6)) =A0# number of 20 cent coins >>>> d =3D amb(range(3)) =A0# number of 50 cent coins >>>> for _ in amb(lambda a,b,c,d: 5*a+10*b+20*c+50*d =3D=3D 45): > ... =A0 =A0 print a, b, c, d > ... > 3 1 1 0 > 3 3 0 0 > 5 0 1 0 > 5 2 0 0 > 7 1 0 0 > 9 0 0 0 > > > Suppose I have some words, and want to put them together so that there > are a certain number of vowels: > >>>> amb =3D Amb() >>>> a =3D amb(['quick', 'slow', 'hungry', 'wise-old']) >>>> b =3D amb(['fox', 'hare', 'turtle', 'kangaroo']) >>>> c =3D amb(['lazy', 'stupid', 'sleepy', 'confused']) >>>> d =3D amb(['dog', 'aardvark', 'sloth', 'wombat']) >>>> >>>> def test(a, b, c, d): > ... =A0 =A0 s =3D "The %s brown %s jumped over the %s %s." % (a, b, c, d) > ... =A0 =A0 num_vowels =3D sum(s.count(c) for c in 'aeiou') > ... =A0 =A0 return num_vowels in (12, 18, 19) > ... >>>> for _ in amb(test): > ... =A0 =A0 print a, b, c, d > ... > quick fox lazy sloth > quick fox lazy dog > quick kangaroo stupid aardvark > [...more solutions cut for brevity] > hungry kangaroo confused aardvark > > > > As written, amb is just a brute-force solver using more magic than is > good for any code, but it's fun to play with. I use a similar technique *a lot* for various kinds of constraint solvers, but I have yet to come up with a really satisfying spelling for it. Have you looked at the way this is done in Sage? Geremy Condra