Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Created May 19, 2014 01:16
Show Gist options
  • Save adammcarth/6676c82f3db4b5419ad5 to your computer and use it in GitHub Desktop.
Save adammcarth/6676c82f3db4b5419ad5 to your computer and use it in GitHub Desktop.
Algorithm for umm... Rockon
prices_hash = { "large" => 12, "medium" => 10, "small" => 8 }

Function calculate_cost(num_rocks, rock_sizes=[])
  // Workout base cost
  cost = 0
  For Each (rock_sizes) as |size|
    cost = cost + prices_hash[size]
  End Loop

  // Apply a 15% discount if 3 or more rocks are ordered
  IF num_rocks > 2
    cost = cost - (cost * 0.15)
  ENDIF

  // Add a 15% surcharge if a large rock is ordered
  IF rock_sizes Includes "large"
    cost = cost + (cost * 0.15)
  ENDIF
End Function

Function calculate_shipping(num_rocks, state)
  // Apply the accounting principle of conservatism in case an unknown state arrises
  shipping = 20

  // Apply default shipping rates to the shipping variable
  IF state = "act" or "nsw"
    shipping = 5
  elseif state = "nt" or "qld"
    shipping = 10
  elseif state = "sa" or "vic" or "wa"
    shipping = 15
  elseif state = "tas"
    shipping = 20
  ENDIF

  // Double the shipping costs if there are more than 5 rocks in the order
  IF num_rocks > 5
    shipping = shipping * 2
  ENDIF

  // Return the total shipping cost or FLASE if there are more than 10 items
  // (we can't ship anymore than 10 items at once!).
  IF num_rocks < 10
    return shipping
  ELSE
    return false
  ENDIF
End Function

counter = 0
For Each (Input Rock_Sizes)
  counter = counter + 1
End Loop

total_rock_price = calculate_cost(counter, Input Rock_Sizes)
total_shipping_price = calculate_shipping(counter, Input State)
grandtotal = total_rock_price + total_shipping_price

// Apply a 20% discount if the coupon code is supplied
IF Input Discount == "rockon"
  grandtotal = grandtotal - (grandtotal * 0.2)
ENDIF

// Show our great work to the world...
PRINT "The rocks cost you $#{total_rock_price}, with a total shipping price of $#{total_shipping_price}. Your grand total is $#{grandtotal}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment