在这段生成的集合代码为商品目录建立索引前审查它。
返回最多两个启用商品的按名称排序索引,不得修改输入,并且要拒绝重复 SKU。
kotlin
data class Product(
val sku: String,
val name: String,
val active: Boolean,
)
fun activeIndex(products: MutableList<Product>): Map<String, Product> {
val sorted = products
sorted.sortBy { it.name }
val selected = mutableListOf<Product>()
for (index in sorted.indices) {
val product = sorted.drop(index).first()
if (!product.active) continue
selected += product
if (selected.size == 2) break
}
return selected.associateBy { it.sku }
}
生成代码仅作示例,不代表任何特定模型