Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually, I've refactored my algorithm some more. There were several correct combinations I was missing, and it was due to the fact that I was returning as soon as the cost went over 15.05 -- I wasn't bothering to check other (cheaper) items that I could add. Here's my new algorithm:</p> <pre><code>&lt;cffunction name="testCombo" returntype="numeric"&gt; &lt;cfargument name="currentCombo" type="string" required="true" /&gt; &lt;cfargument name="currentTotal" type="numeric" required="true" /&gt; &lt;cfargument name="apps" type="array" required="true" /&gt; &lt;cfset var a = 0 /&gt; &lt;cfset var found = false /&gt; &lt;cfset var CC = "" /&gt; &lt;cfset var CT = 0 /&gt; &lt;cfset tries = tries + 1 /&gt; &lt;cfloop from="1" to="#arrayLen(arguments.apps)#" index="a"&gt; &lt;cfset combos = combos + 1 /&gt; &lt;cfset CC = listAppend(arguments.currentCombo, arguments.apps[a].name) /&gt; &lt;cfset CT = arguments.currentTotal + arguments.apps[a].cost /&gt; &lt;cfif CT eq 15.05&gt; &lt;!--- print current combo ---&gt; &lt;cfoutput&gt;&lt;strong&gt;#CC# = 15.05&lt;/strong&gt;&lt;/cfoutput&gt;&lt;br /&gt; &lt;cfreturn true /&gt; &lt;cfelseif CT gt 15.05&gt; &lt;!--&lt;cfoutput&gt;#arguments.currentCombo# &gt; 15.05 (aborting)&lt;/cfoutput&gt;&lt;br /&gt;--&gt; &lt;cfelse&gt; &lt;!--- less than 15.50 ---&gt; &lt;!--&lt;cfoutput&gt;#arguments.currentCombo# &lt; 15.05 (traversing)&lt;/cfoutput&gt;&lt;br /&gt;--&gt; &lt;cfset found = testCombo(CC, CT, arguments.apps) /&gt; &lt;/cfif&gt; &lt;/cfloop&gt; &lt;cfreturn found /&gt; &lt;/cffunction&gt; &lt;cfset mf = {name="Mixed Fruit", cost=2.15} /&gt; &lt;cfset ff = {name="French Fries", cost=2.75} /&gt; &lt;cfset ss = {name="side salad", cost=3.35} /&gt; &lt;cfset hw = {name="hot wings", cost=3.55} /&gt; &lt;cfset ms = {name="moz sticks", cost=4.20} /&gt; &lt;cfset sp = {name="sampler plate", cost=5.80} /&gt; &lt;cfset apps = [ mf, ff, ss, hw, ms, sp ] /&gt; &lt;cfset tries = 0 /&gt; &lt;cfset combos = 0 /&gt; &lt;cfoutput&gt; &lt;cfloop from="1" to="6" index="b"&gt; #testCombo(apps[b].name, apps[b].cost, apps)# &lt;/cfloop&gt; &lt;br /&gt; tries: #tries#&lt;br /&gt; combos: #combos# &lt;/cfoutput&gt; </code></pre> <p>Output:</p> <pre><code>Mixed Fruit,Mixed Fruit,Mixed Fruit,Mixed Fruit,Mixed Fruit,Mixed Fruit,Mixed Fruit = 15.05 Mixed Fruit,hot wings,hot wings,sampler plate = 15.05 Mixed Fruit,hot wings,sampler plate,hot wings = 15.05 Mixed Fruit,sampler plate,hot wings,hot wings = 15.05 false false false hot wings,Mixed Fruit,hot wings,sampler plate = 15.05 hot wings,Mixed Fruit,sampler plate,hot wings = 15.05 hot wings,hot wings,Mixed Fruit,sampler plate = 15.05 hot wings,sampler plate,Mixed Fruit,hot wings = 15.05 false false sampler plate,Mixed Fruit,hot wings,hot wings = 15.05 sampler plate,hot wings,Mixed Fruit,hot wings = 15.05 false tries: 2014 combos: 12067 </code></pre> <p>I think this may have all of the correct combinations, but my question still stands: Is there a better algorithm?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload