While, Ray is still reeling from the massive response to his IPhone post which has pushed back the Lemonade Stand Puzzler, I thought I would take a quick opportunity to put my code out there. In overview, Ray asked us to write a function that emulated a Lemonade Stand. It had to decide how many cups of lemonade to sell and for what price based on the weather, temperature, budget, and cost of supplies.Here is the run-down my code:
  • First, I establish baseline numbers for an average day of business
    • What percentage of my total funds to invest (don't want to spend it all at once)
    • What kind of mark up over my costs should I apply for profit
  • Then, based on the temperature for the day, I establish an index for how interested my customers will be and how much they will be willing to pay.
    For instance, if it's under 40 degrees, no one will buy, but if it is above 100, people might sell their first born.
  • Next, based on the weather I determine an index for my risk that day.
    Sunny is very low risk, stormy is a very high risk.
  • The business interest minus the risk helps create an overall business adjustment for the day which I will apply to my baseline amounts.
  • I calculate what percentage of my budget I want to invest.
  • I calculate how much to mark up my product that day.
  • I calculate my price per cup given today's cost and markup.
  • I calculate how many cups I can make given today's price and investment.
So, basically, the colder and stormier it gets, the less I make and the less I mark it up because not as many people will be buying and I need low prices to attract customers. The hotter and sunnier it gets, the more I make and the more I mark up the price. Of course, if it is raining, but over 100, the customer interest will cancel out some of the risk. At any rate, here's he code:
[code]
<!--- 
Name:	Lemonade Stand 
Author:	Brad Wood
Date:	07/08/2008 
--->

<cffunction name="ls_BradWood" output="false" returnType="struct" hint="Brad Wood">
	<cfargument name="predictedtemp" type="numeric" required="true" hint="Predicted high temp.">
	<cfargument name="predictedweather" type="string" required="true" hint="Predicted weather. (sunny, clear, rain, or storm)">
	<cfargument name="cupprice" type="numeric" required="true" hint="Production price. (3 to 8 cents)">
	<cfargument name="budget" type="numeric" required="true" hint="Amount of money available to spend.">

	<cfset var local = structnew()>	
	<cfset var return_struct = structnew()>
	<!--- Baseline markup is 250% above cost --->
	<cfset local.baseline_markup = 2.50>
	<!--- Baseline investment is 65% of budget --->
	<cfset local.baseline_investment = .65>
	
	<!--- Determine the market interest in your product --->
	<cfif arguments.predictedtemp LT 40>
		<!--- Not many people will be buying-- consider hot chocolate --->
		<cfset local.market_interest_for_today = -3>
	<cfelseif arguments.predictedtemp GTE 40 and arguments.predictedtemp LT 60>
		<!--- Minor interest --->
		<cfset local.market_interest_for_today = -2>
	<cfelseif arguments.predictedtemp GTE 60 and arguments.predictedtemp LT 80>
		<!--- Getting there --->
		<cfset local.market_interest_for_today = 0>
	<cfelseif arguments.predictedtemp GTE 80 and arguments.predictedtemp LT 100>
		<!--- Standing in line... --->
		<cfset local.market_interest_for_today = 2>
	<cfelseif arguments.predictedtemp GTE 100>
		<!--- I'll pay ANYTHING for that! --->
		<cfset local.market_interest_for_today = 4>
	</cfif>
	
	<!--- Calculate the risk for today based on weather. --->
	<cfswitch expression="#arguments.predictedweather#">
		<cfcase value="sunny">
			<cfset local.risk_for_today = -1>
		</cfcase>
		<cfcase value="clear">
			<cfset local.risk_for_today = 0>
		</cfcase>
		<cfcase value="rain">
			<cfset local.risk_for_today = 2>
		</cfcase>
		<cfcase value="storm">
			<cfset local.risk_for_today = 4>
		</cfcase>
		<cfdefaultcase>
			<cfset local.risk_for_today = 0>		
		</cfdefaultcase>
	</cfswitch>
	
	<!--- Calculate Business adjustment for today based on risk and product interest. --->
	<cfset local.overall_business_adjustment_for_today = (local.market_interest_for_today - local.risk_for_today) * .1>
	
	<!--- Figure today's investement --->
	<cfset local.max_amount_to_invest_today = max(0,min(arguments.budget,arguments.budget * (local.baseline_investment + local.overall_business_adjustment_for_today)))>
	<!--- Figure how much to mark up the price today --->
	<cfset local.markup_for_today = max(1.25,local.baseline_markup + (local.baseline_markup * local.overall_business_adjustment_for_today))>
	<!--- Given today's markup, what will my price per cup be? --->
	<cfset local.price_per_cup_for_today = arguments.cupprice * local.markup_for_today>
	<!--- How many cups can I make at that price? --->
	<cfset local.number_of_cups_for_today = min(250,local.max_amount_to_invest_today / local.price_per_cup_for_today)>
	
	<!--- Testing Output budget
	<cfset return_struct.stats.overall_business_adjustment_for_today = local.overall_business_adjustment_for_today>
	<cfset return_struct.stats.max_amount_to_invest_today = local.max_amount_to_invest_today>
	<cfset return_struct.stats.markup_for_today = local.markup_for_today> --->
	<cfset return_struct.pricepercup = fix(local.price_per_cup_for_today)>
	<cfset return_struct.numberofcups = fix(local.number_of_cups_for_today)>
		
	<cfreturn return_struct>

</cffunction>
[/code]