Menu
Coddy logo textTech

Subset Checks

Part of the Logic & Flow section of Coddy's Kotlin journey. Lesson 14 of 63.

available.containsAll(required) checks whether every required member is available. Duplicate entries in a required list do not demand multiple copies because this is a membership check. An empty required collection is always contained.

Inside main:

val available = setOf("pen", "pad")
println(available.containsAll(listOf("pen", "pen")))

Both requirements ask for the same membership, which is available.

containsAll checks membership, not how many copies are available.

challenge icon

Challenge

Medium

Complete canCover with parameters available: List<String>, required: List<String>. Return whether every required item is present among available items. This is a membership problem, not a quantity problem.

Unless stricter bounds are stated above, collections contain at most 100 elements at each level, and integer arguments and integer collection values are between -1000 and 1000. Text supplied for parsing can include invalid or out-of-range representations as described.

Return a value of type Boolean. Keep the supplied input/output code. It reads scalar arguments one per line; a list starts with its count followed by its elements, and a map starts with its entry count followed by each key and value. Nested lists repeat the count-and-elements format for each row. The supplied main prints the return value followed by one newline. Lists use Kotlin's standard bracketed format; print no additional labels.

Try it yourself

fun canCover(available: List<String>, required: List<String>): Boolean {
    // Write your solution here.
    return false
}
fun main() {
    val availableCount = readln().toInt()
    val available = mutableListOf<String>()
    for (i0 in 0 until availableCount) {
        val availableItem = readLine().orEmpty()
        available.add(availableItem)
    }
    val requiredCount = readln().toInt()
    val required = mutableListOf<String>()
    for (i0 in 0 until requiredCount) {
        val requiredItem = readLine().orEmpty()
        required.add(requiredItem)
    }
    println(canCover(available, required))
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow

Practice on your own: Kotlin playground