Skip to content

Instantly share code, notes, and snippets.

@biodunalfet
Created June 25, 2019 11:48
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 biodunalfet/38b69a3307c9b74ce80224557d462e6f to your computer and use it in GitHub Desktop.
Save biodunalfet/38b69a3307c9b74ce80224557d462e6f to your computer and use it in GitHub Desktop.
fun price(items : String) : Double {
//split string into items
//store in a map thats maps items to their quantity
//calculate price
val itemsAsList = items.toCharArray().map { it.toString() }
val itemsMap = mutableMapOf<String, Int>()
for (i in itemsAsList) {
val qty = itemsMap.getOrDefault(i, 0)
itemsMap[i] = qty + 1
}
print(itemsMap)
var price = 0.0
for ((key, value) in itemsMap) {
when(key) {
"A" -> {
val groupA = value/3
val remA = value % 3
price += groupA * 130
price += remA * 50
}
"B" -> {
val groupB = value/2
val remB = value % 2
price += groupB * 45
price += remB * 30
}
"C" -> {
price += value * 20
}
"D" -> {
price += value * 15
}
else -> {
price += 0
}
}
}
return price
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment