Last active
October 11, 2017 20:39
-
-
Save angelo510/5c13694c75eb7d0c17eee61752ffabf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inputProducts = gets | |
total = 0 | |
products = { | |
"A" => { "1" => 2.00, "4" => 7.00 }, | |
"B" => { "1" => 12.00 }, | |
"C" => { "1" => 1.25, "6" => 6.00 }, | |
"D" => { "1" => 0.15 } | |
} | |
inputArray = inputProducts.split('') # input products | |
counts = Hash.new # number of item | |
products.each_key do |key| | |
counts[key] = inputArray.count(key) | |
end | |
total = 0.0 | |
counts.each_key do |key| | |
if products[key].count > 1 | |
groupKey = products[key].keys[1] | |
groupUnit = counts[key] / groupKey.to_i | |
singleCount = counts[key] % groupKey.to_i | |
subtotal = groupUnit * products[key][groupKey] + singleCount * products[key]['1'] | |
total += subtotal | |
else | |
subtotal = counts[key] * products[key]['1'] | |
total += subtotal | |
end | |
puts "Subtotal: #{subtotal}" | |
end | |
puts "Final Total: #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment