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


Groups > comp.lang.python > #4842

Re: Coolest Python recipe of all time

References <69c1813d-1a9a-4686-9768-8ec1910a45f8@d19g2000prh.googlegroups.com> <4dc428f2$0$29991$c3e8da3$5496439d@news.astraweb.com>
Date 2011-05-06 10:43 -0700
Subject Re: Coolest Python recipe of all time
From geremy condra <debatem1@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1257.1304703790.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 6, 2011 at 9:59 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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 = Amb()
>>>> a = amb(range(3, 21))  # number of 5 cent coins
>>>> b = amb(range(11))  # number of 10 cent coins
>>>> c = amb(range(6))  # number of 20 cent coins
>>>> d = amb(range(3))  # number of 50 cent coins
>>>> for _ in amb(lambda a,b,c,d: 5*a+10*b+20*c+50*d == 45):
> ...     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 = Amb()
>>>> a = amb(['quick', 'slow', 'hungry', 'wise-old'])
>>>> b = amb(['fox', 'hare', 'turtle', 'kangaroo'])
>>>> c = amb(['lazy', 'stupid', 'sleepy', 'confused'])
>>>> d = amb(['dog', 'aardvark', 'sloth', 'wombat'])
>>>>
>>>> def test(a, b, c, d):
> ...     s = "The %s brown %s jumped over the %s %s." % (a, b, c, d)
> ...     num_vowels = sum(s.count(c) for c in 'aeiou')
> ...     return num_vowels in (12, 18, 19)
> ...
>>>> for _ in amb(test):
> ...     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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-02 10:33 -0700
  Re: Coolest Python recipe of all time David Monaghan <monaghand.david@gmail.com> - 2011-05-02 21:48 +0100
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-02 14:58 -0600
      Re: Coolest Python recipe of all time David Monaghan <monaghand.david@gmail.com> - 2011-05-02 22:45 +0100
        Re: Coolest Python recipe of all time Stefan Behnel <stefan_ml@behnel.de> - 2011-05-03 07:04 +0200
          Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 09:43 -0700
            Re: Coolest Python recipe of all time Chris Angelico <rosuav@gmail.com> - 2011-05-04 07:54 +1000
            Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-03 16:10 -0600
        Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-02 23:17 -0600
        Re: Coolest Python recipe of all time Terry Reedy <tjreedy@udel.edu> - 2011-05-03 02:00 -0400
          Re: Coolest Python recipe of all time Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-03 18:29 +1200
            Re: Coolest Python recipe of all time Terry Reedy <tjreedy@udel.edu> - 2011-05-03 11:49 -0400
            Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 09:32 -0700
            Re: Coolest Python recipe of all time geremy condra <debatem1@gmail.com> - 2011-05-03 09:51 -0700
        Re: Coolest Python recipe of all time Stefan Behnel <stefan_ml@behnel.de> - 2011-05-03 08:23 +0200
          Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 15:19 -0700
  Re: Coolest Python recipe of all time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 16:59 +0000
    Re: Coolest Python recipe of all time geremy condra <debatem1@gmail.com> - 2011-05-06 10:43 -0700
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 12:36 -0600
      Re: Coolest Python recipe of all time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-07 08:29 +0000
        Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-07 08:54 -0600
        Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-07 14:02 -0700
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 13:38 -0600
    Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-06 12:58 -0700
  RE: Coolest Python recipe of all time Trent Nelson <trent@snakebite.org> - 2011-05-09 02:31 -0700
    Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-09 14:10 -0700

csiph-web