So I don't know if this is cheating or not. You guys can tell me. My son's teacher assigns some homework that is meant to be done with a parent. Today he had a problem that involved school lunches. The problem was that if a student spent exactly 5 dollars for lunch, he would get a free cookie. There were four groups of foods and each item had a different price. You had to pick one item from each group and reach a sum of 5 dollars. He found one by himself, and had trouble finding the other combinations. We began to do it by random guessing, and quickly gave up. It then occurred to me. This was a perfect example of boring, iterative work that a computer could probably solve in one second. I created a set of data for the food groups and items:

<cfset meat = {chicken=1.95,roast_beef=3.05,shrimp=3.50,roast_pork=2.75}> <cfset salad = {cole_slaw=0.60,potato_salad=0.95,dinner_salad=0.75,macaroni_salad=1.10}> <cfset veggies_potatoes = {mashed_potatoes=1,french_fries=0.85,sweet_corn=0.65,green_beans=0.5}> <cfset drinks = {milk=0.40,chocolate_milk=0.45,oj=0.95,soda=0.55}>

In case you are wondering about the key names, when you use implicit struct notation you can't use strings for the key names. I had tried "roast beef"=3.05, but that returned a syntax error.

So that defined the data, now I just needed to loop and check the prices:

<cfloop item="m" collection="#meat#"> <cfloop item="s" collection="#salad#"> <cfloop item="v" collection="#veggies_potatoes#"> <cfloop item="d" collection="#drinks#"> <cfset combo_price = meat[m] + salad[s] + veggies_potatoes[v] + drinks[d]> <cfif combo_price is 5> <cfoutput>#m#+#s#+#v#+#d#<br /></cfoutput> </cfif> </cfloop> </cfloop> </cfloop> </cfloop>

I walked through the code with my son and made sure he got what was going on. We then ran it and got the answers:


We then double checked each combination to ensure it added up right. So - is that cheating? I say any early introduction to the power of programming is never a bad thing! :)