Sha256: 696cb6807cf13fc0925ce1501ad3d1368e37efd9bdab8c891757c1485b616529

Contents?: true

Size: 593 Bytes

Versions: 188

Compression:

Stored size: 593 Bytes

Contents

fun <T> List<T>.relationshipTo(list: List<T>): Relationship {
    if (this == list)         return Relationship.EQUAL
    if (this.isSublist(list)) return Relationship.SUBLIST
    if (list.isSublist(this)) return Relationship.SUPERLIST

    return Relationship.UNEQUAL
}

private fun <T> List<T>.isSublist(list: List<T>): Boolean {
    val listSize = list.size

    if (size > listSize) return false

    val numberOfSublistCandidates = listSize - size + 1

    return (0 until numberOfSublistCandidates).any { startIndex ->
        list.subList(startIndex, startIndex + size) == this
    }
}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.58 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.57 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.56 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.55 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.54 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.53 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.52 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.51 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.50 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.49 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.48 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.47 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.46 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.45 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.44 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.43 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.42 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.41 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.40 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.39 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt