<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: On ordering appetizers</title>
	<atom:link href="http://peter.makholm.net/2007/07/09/on-ordering-appetizers/feed/" rel="self" type="application/rss+xml" />
	<link>http://peter.makholm.net/2007/07/09/on-ordering-appetizers/</link>
	<description></description>
	<pubDate>Fri, 21 Nov 2008 19:39:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Peter Makholm</title>
		<link>http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17300</link>
		<dc:creator>Peter Makholm</dc:creator>
		<pubDate>Wed, 11 Jul 2007 07:06:58 +0000</pubDate>
		<guid isPermaLink="false">http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17300</guid>
		<description>Not just a Python version but a quite different solution. I don't think it'is you data structure but an much better algorithm that makes the difference. Reimplementing it in perl with a dumb list gives a comparable speed:

&lt;pre&gt;&lt;code&gt;
#!/usr/bin/perl

$, = " ";

@p = (580, 420, 355, 335, 275, 215);
$target = 1505;

my @solution = ([0]);
while (($val,@data) = @{ shift @solution }) {
        next if $val &gt; $target;
        do { print @data; last } if $val == $target;

        push @solution, [$val + $_, @data, $_] for @p;
}
&lt;/code&gt;&lt;/pre&gt;

For harder problems and for a general solution which is to be run again and again speed will be an issue. But for solving a specific problem I couldn't care less if it takes a minute or two to run - it's all about getting things done.

But my original code is crude, brute and ignorant and probally didn't deserved to be made public.</description>
		<content:encoded><![CDATA[<p>Not just a Python version but a quite different solution. I don&#8217;t think it&#8217;is you data structure but an much better algorithm that makes the difference. Reimplementing it in perl with a dumb list gives a comparable speed:</p>
<pre><code>
#!/usr/bin/perl

$, = " ";

@p = (580, 420, 355, 335, 275, 215);
$target = 1505;

my @solution = ([0]);
while (($val,@data) = @{ shift @solution }) {
        next if $val > $target;
        do { print @data; last } if $val == $target;

        push @solution, [$val + $_, @data, $_] for @p;
}
</code></pre>
<p>For harder problems and for a general solution which is to be run again and again speed will be an issue. But for solving a specific problem I couldn&#8217;t care less if it takes a minute or two to run - it&#8217;s all about getting things done.</p>
<p>But my original code is crude, brute and ignorant and probally didn&#8217;t deserved to be made public.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Urlichs</title>
		<link>http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17284</link>
		<dc:creator>Matthias Urlichs</dc:creator>
		<pubDate>Tue, 10 Jul 2007 18:01:41 +0000</pubDate>
		<guid isPermaLink="false">http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17284</guid>
		<description>Sorry about the indent lossage.

indent everything below while: once, everything below for: twice, and the print+break twice.</description>
		<content:encoded><![CDATA[<p>Sorry about the indent lossage.</p>
<p>indent everything below while: once, everything below for: twice, and the print+break twice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Urlichs</title>
		<link>http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17283</link>
		<dc:creator>Matthias Urlichs</dc:creator>
		<pubDate>Tue, 10 Jul 2007 17:59:20 +0000</pubDate>
		<guid isPermaLink="false">http://peter.makholm.net/2007/07/09/on-ordering-appetizers/#comment-17283</guid>
		<description>Well, here's a Python version. It happens to be a whole lot faster than yours.
Using appropriate data structures helps, I would say. ;-)

It prefers the shortest solution because the pqueue module is a FIFO for equal amounts (aka priority values).

#!/usr/bin/python
import pqueue

p = (580, 420, 355, 335, 275, 215)
target = 1505

q = pqueue.PQueue()
q.insert(0,(0,)*len(p))

current = -1
while True:
        val,data = q.pop()
        if val == target:
                print [a for a in zip(data,p) if a[0]]
                break
        if current == val: continue
        current = val
        data = list(data)
        for f in range(len(p)):
                val += p[f]
                data[f] += 1
                q.insert(val,tuple(data))
                data[f] -= 1
                val -= p[f]</description>
		<content:encoded><![CDATA[<p>Well, here&#8217;s a Python version. It happens to be a whole lot faster than yours.<br />
Using appropriate data structures helps, I would say. ;-)</p>
<p>It prefers the shortest solution because the pqueue module is a FIFO for equal amounts (aka priority values).</p>
<p>#!/usr/bin/python<br />
import pqueue</p>
<p>p = (580, 420, 355, 335, 275, 215)<br />
target = 1505</p>
<p>q = pqueue.PQueue()<br />
q.insert(0,(0,)*len(p))</p>
<p>current = -1<br />
while True:<br />
        val,data = q.pop()<br />
        if val == target:<br />
                print [a for a in zip(data,p) if a[0]]<br />
                break<br />
        if current == val: continue<br />
        current = val<br />
        data = list(data)<br />
        for f in range(len(p)):<br />
                val += p[f]<br />
                data[f] += 1<br />
                q.insert(val,tuple(data))<br />
                data[f] -= 1<br />
                val -= p[f]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
