Untitled

mail@pastecode.io avatar
unknown
kotlin
7 months ago
991 B
2
Indexable
Never
import java.io.File

private const val PATH_TO_FILE = "C:\\Users\\Lenovo\\Desktop\\phone_book.txt"

fun main(args: Array<String>) {
    val phoneNumbers = getPhoneNumbers()

    val newPhoneNumWithCode = getPhoneNumWithCodeOnly(phoneNumbers[0])
    val oldPhoneNumbers = phoneNumbers
        .drop(1)

    for (oldNum in oldPhoneNumbers) {
        val verdict = if (getPhoneNumWithCodeOnly(oldNum) == newPhoneNumWithCode) {
            "YES"
        } else {
            "NO"
        }

        println("$oldNum $verdict")
    }

    println("Input phone: ${phoneNumbers[0]}")
}

fun getPhoneNumWithCodeOnly(phoneNumber: String): String {
    val phoneDigits = phoneNumber.filter { it.isDigit() }
    return if (phoneDigits.length < 11) {
        "495$phoneDigits"
    } else {
        // выкидываем код страны
        phoneDigits.substring(1)
    }
}

fun getPhoneNumbers(): List<String> {
    return File(PATH_TO_FILE).readLines()
}
Leave a Comment