Skip to main content
added 27 characters in body
Source Link
sealed class MasterComparisonResult {
    object DifferentLength : MasterComparisonResult()
    
    data class SameLength(val commonAtSameIndex: Int, val commonOverall: Int) : MasterComparisonResult()
}
sealed class MasterComparisonResult {
    object DifferentLength : MasterComparisonResult()
    
    data class SameLength(val commonAtSameIndex: Int, val commonOverall: Int)
}
sealed class MasterComparisonResult {
    object DifferentLength : MasterComparisonResult()
    
    data class SameLength(val commonAtSameIndex: Int, val commonOverall: Int) : MasterComparisonResult()
}
deleted 1574 characters in body
Source Link

Here's an alternative solution that uses a bitmask:

fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }
                val countedAlready = BooleanArray(length)
                val commonOverall  = map {
                    character -> otherString
                            .withIndex()
                            .filterNot {
                                (index, _) -> countedAlready[index]
                            }
                            .firstOrNull {
                                (_, value) -> character == value
                            }
                            ?.index
                            ?.also {
                                countedAlready[it] = true
                            }
                        }
                        .count { it != null }
                commonOverall to commonAtSameIndex
            }
        }

And finally a version that combines the functional way - in my opinion much clearer - of calculating the number of identical characters in the same positions with the loop that calculates the other value.

fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }

                var commonOverall: Int = 0
                val countedAlready = BooleanArray(length)   

                /* this is yet another detail: there's no need for toList() allocation.
                 * you can iterate over this (String) straight away. */
                for (c in this) {
                    /* find the first occurrence of the c character in otherString
                     * that wasn't counted already */
                    val index = otherStringcountedAlready
                                    .asSequence()
                                    .withIndex()
                                    .filterfilterNot {
                                        countedAlready[it.index].not() && it.value == c
                                    }
                                    .mapindexOfFirst {
                                        itotherString[it.index
                                 index] == c }
                                    .firstOrNull()
                    if (index !=>= null0) {
                        commonOverall++
countedAlready[index] = true
                      countedAlready[index] = truecommonOverall++
                    }
                }
                commonOverall to commonAtSameIndex
            }
        }

Here's an alternative solution that uses a bitmask:

fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }
                val countedAlready = BooleanArray(length)
                val commonOverall  = map {
                    character -> otherString
                            .withIndex()
                            .filterNot {
                                (index, _) -> countedAlready[index]
                            }
                            .firstOrNull {
                                (_, value) -> character == value
                            }
                            ?.index
                            ?.also {
                                countedAlready[it] = true
                            }
                        }
                        .count { it != null }
                commonOverall to commonAtSameIndex
            }
        }

And finally a version that combines the functional way - in my opinion much clearer - of calculating the number of identical characters in the same positions with the loop that calculates the other value.

fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }

                var commonOverall: Int = 0
                val countedAlready = BooleanArray(length)   

                /* this is yet another detail: there's no need for toList() allocation.
                 * you can iterate over this (String) straight away. */
                for (c in this) {
                    /* find the first occurrence of the c character in otherString
                     * that wasn't counted already */
                    val index = otherString
                                    .asSequence()
                                    .withIndex()
                                    .filter {
                                        countedAlready[it.index].not() && it.value == c
                                    }
                                    .map {
                                        it.index
                                    }
                                    .firstOrNull()
                    if (index != null) {
                        commonOverall++
                        countedAlready[index] = true
                    }
                }
                commonOverall to commonAtSameIndex
            }
        }

Here's a version that combines the functional way - in my opinion much clearer - of calculating the number of identical characters in the same positions with the loop that calculates the other value.

fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }

                var commonOverall = 0
                val countedAlready = BooleanArray(length)   

                /* this is yet another detail: there's no need for toList() allocation.
                 * you can iterate over this (String) straight away. */
                for (c in this) {
                    /* find the first occurrence of the c character in otherString
                     * that wasn't counted already */
                    val index = countedAlready
                            .asSequence()
                            .withIndex()
                            .filterNot { it.value }
                            .indexOfFirst { otherString[it.index] == c }
                    if (index >= 0) {
                        countedAlready[index] = true
                        commonOverall++
                    }
                }
                commonOverall to commonAtSameIndex
            }
        }
edited body
Source Link
fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }
                val countedAlready = BooleanArray(length)
                val commonOverall  = map {
                    character -> otherString
                            .withIndex()
                            .filterNot {
                                (index, _) -> countedAlready[index]
                            }
                            .firstOrNull {
                                (_, value) -> character == value
                            }
                            ?.index
                            ?.also {
                                countedAlready[it] = true
                            } != null
                        }
                        .count { it != null }
                commonOverall to commonAtSameIndex
            }
        }
fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }
                val countedAlready = BooleanArray(length)
                val commonOverall  = map {
                    character -> otherString
                            .withIndex()
                            .filterNot {
                                (index, _) -> countedAlready[index]
                            }
                            .firstOrNull {
                                (_, value) -> character == value
                            }
                            ?.index
                            ?.also {
                                countedAlready[it] = true
                            } != null
                        }
                        .count { it }
                commonOverall to commonAtSameIndex
            }
        }
fun String.masterCompare(otherString: String): Pair<Int, Int>? =
        when {
            length != otherString.length -> null
            else -> {
                // this part is really easy
                val commonAtSameIndex = zip(otherString).count {
                    (one, another) -> one == another
                }
                val countedAlready = BooleanArray(length)
                val commonOverall  = map {
                    character -> otherString
                            .withIndex()
                            .filterNot {
                                (index, _) -> countedAlready[index]
                            }
                            .firstOrNull {
                                (_, value) -> character == value
                            }
                            ?.index
                            ?.also {
                                countedAlready[it] = true
                            }
                        }
                        .count { it != null }
                commonOverall to commonAtSameIndex
            }
        }
added 1942 characters in body
Source Link
Loading
added 1295 characters in body
Source Link
Loading
Source Link
Loading