Skip to content

Instantly share code, notes, and snippets.

@luca-aurelia
Created June 17, 2015 00:49
Show Gist options
  • Save luca-aurelia/732d83fee49c72f6c8d8 to your computer and use it in GitHub Desktop.
Save luca-aurelia/732d83fee49c72f6c8d8 to your computer and use it in GitHub Desktop.
# This is a small program that compares two different
# pay-out scenarios offered to the indie funk
# band Partial Cinema. http://partialcinema.com
# Given ticket sales numbers, compare the pay-out
# and return-on-investment (dollars received per ticket sold)
# for the two different scenarios.
#
# tickets - an array of the form [integer, integer, integer]
# where each integer represents the number of tickets
# sold by a band. the first integer represents the
# number of tickets sold by Partial Cinema. the second
# and third integers represent the number of tickets sold
# by whatever bands happen to be playing with PC.
analyze = (tickets) ->
console.log "Assuming these ticket sales:"
console.log formatNicely ticketSummary tickets
console.log "---"
console.log ""
# the first gig we were offered
analyzeGig "Hard Rock Cafe", tickets, hardRockProfits
# the second gig we were offered
analyzeGig "Smith's Old Bar", tickets, smithsProfits
# analyze a particular pay-out scenario and
# print the relevant information.
#
# title - the name of the gig to print
# tickets - an array of integers; the number at each index
# represents the amount of tickets sold by each band
# calcProfits - a function that takes ticket sales
# and computes the profit for each band
analyzeGig = (title, tickets, calcProfits) ->
console.log "Analysis for #{title}"
console.log ""
profits = calcProfits tickets
console.log "\tProfits:"
console.log "\t" + formatNicely profits
rois = calcReturnsOnInvestment(tickets, profits)
console.log "\tReturn on investment:"
console.log "\t" + formatNicely rois
console.log "---"
console.log ""
# calculate return-on-investment,
# measured by number of dollars a
# band receives per ticket sold
calcReturnsOnInvestment = (tickets, profits) ->
# calculate ROI for one band
roi = (ticketsSold, profit) ->
# the prepended plus sign converts the result toFixed to a number
dollarsPerTicket = +(profit / ticketsSold).toFixed(2)
"$" + dollarsPerTicket + " / ticket"
"Partial Cinema": roi(tickets[0], profits["Partial Cinema"])
"Band 2": roi tickets[1], profits["Band 2"]
"Band 3": roi tickets[2], profits["Band 3"]
# calculates each band's profits for the gig
# at Smith's Old Bar in Atlanta, Georgia
#
# tickets - see above
smithsProfits = (tickets) ->
ticketRevenue = (n * 10 for n in tickets) # tickets cost 10 USD
totalProfits = sum(ticketRevenue) - 300
totalProfits = Math.max 0, totalProfits # clamp to a minimum of zero
"total (all bands)": totalProfits * 0.9
"Partial Cinema": totalProfits * 0.3
"Band 2": totalProfits * 0.3
"Band 3": totalProfits * 0.3
# calculates each band's profits for the gig
# at Hard Rock Cafe in Atlanta, Georgia
#
# tickets - see above
hardRockProfits = (tickets) ->
profitForBand = (tix) ->
# each band received $8 for every ticket they sold after the first 10
profit = (tix - 10) * 8
Math.max 0, profit
profits = (profitForBand n for n in tickets)
totalProfit = sum profits
"total (all bands)": totalProfit
"Partial Cinema": profits[0]
"Band 2": profits[1]
"Band 3": profits[2]
# helper methods
# add all the elements in a collection
sum = (collection) ->
collection.reduce (previous, current) -> previous + current
# convert an object to a nicely
# formatted string for easy reading
formatNicely = (obj) ->
stringy = JSON.stringify(obj, undefined, 2)
regEx = /[\{\}\",]/g
stringy.replace(regEx, '')
# in a tickets array, each
# index is implicitly associated
# with a particular band.
#
# this method converts the
# tickets array to an object that
# makes those associations
# explicit.
ticketSummary = (tickets) ->
"Partial Cinema": tickets[0]
"Band 2": tickets[1]
"Band 3": tickets[2]
# actually perform the analysis
ticketSales = [40, 25, 25]
analyze ticketSales
### Sample output
Assuming these ticket sales:
Partial Cinema: 40
Band 2: 25
Band 3: 25
---
Analysis for Hard Rock Cafe
Profits:
total (all bands): 480
Partial Cinema: 240
Band 2: 120
Band 3: 120
Return on investment:
Partial Cinema: $6 / ticket
Band 2: $4.8 / ticket
Band 3: $4.8 / ticket
---
Analysis for Smith's Old Bar
Profits:
total (all bands): 540
Partial Cinema: 180
Band 2: 180
Band 3: 180
Return on investment:
Partial Cinema: $4.5 / ticket
Band 2: $7.2 / ticket
Band 3: $7.2 / ticket
---
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment