Skip to content

Instantly share code, notes, and snippets.

@msimpson
Created August 20, 2011 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msimpson/1158586 to your computer and use it in GitHub Desktop.
Save msimpson/1158586 to your computer and use it in GitHub Desktop.
Tip Calculator
#!/usr/bin/env ruby
# Tip Calculator
# Usage: ./tip <cost> <minutes_waited> [percent]
exit if ARGV.length < 2
cost = ARGV[0].to_f.round(2)
minutes = ARGV[1].to_i
percent = !ARGV[2].nil? ? ARGV[2].to_f / 100.0 : 0.2
max = cost / 40 * 60
scale = ( Math.cos( minutes * Math::PI / (120 * ( cost / 41 ))) * cost * percent )
tip = ( minutes >= max ? 0 : minutes < 5 ? cost - (cost / 5 * minutes) : scale ).round(2)
def pad(n)
return n < 10 ? "0#{n}" : n
end
printf(
"Cost: $%.02f, Wait: %d:%s, Tip: $%0.2f\n",
cost, minutes / 60.floor, pad(minutes % 60), tip
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment