Created
June 25, 2019 11:48
-
-
Save biodunalfet/38b69a3307c9b74ce80224557d462e6f 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
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