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.38 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.37 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.36 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.35 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.34 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.33 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.32 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.31 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.30 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.29 tracks/kotlin/exercises/sublist/.meta/src/reference/kotlin/Sublist.kt
trackler-2.2.1.28 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.27 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.26 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.25 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.24 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.23 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.22 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.21 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.20 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt
trackler-2.2.1.19 tracks/kotlin/exercises/sublist/src/example/kotlin/Sublist.kt