October 2025
Intermediate to advanced
100 pages
2h 27m
English
| Puzzle 21 | Serial Spending Problem |
| | data class Product(val name: String, val cost: Int) |
| | |
| | val shoppingList = listOf( |
| | Product("Eggs", cost = 10), |
| | Product("Flour", cost = 10), |
| | Product("Sugar", cost = 10), |
| | ) |
| | |
| | fun main() { |
| | var moneySpent = 0 |
| | val purchasedItems = mutableListOf<String>() |
| | |
| | shoppingList.forEach { product -> |
| | purchasedItems += product.name |
| | moneySpent += product.cost |
| | if (moneySpent >= 20) { |
| | return@forEach |
| | } |
| | } |
| | |
| | println("Spent a total of $moneySpent to buy $purchasedItems") |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess the output ... |
Read now
Unlock full access