1

So, I have made function that return publicKey and I need to duplicate it x times for later use. For example: publicKey: [76, 152, 93, 102, 145, 181] and I need to duplicate it 3 times, so the end result should be [76, 152, 93, 102, 145, 181, 76, 152, 93, 102, 145, 181, 76, 152, 93, 102, 145, 181]. I have tried using var list3 = publicKey*x, but as you can imagine, it didn't work..

fun getPublicKey(privateKey: ArrayList<BigInteger>, 
                 birthDay: BigInteger, 
                 primNumb: BigInteger): ArrayList<BigInteger> {
    val inversMod = birthDay.modInverse(primNumb)
    //println(inversMod)
    val publicKey: ArrayList<BigInteger> = ArrayList()
    for (keyValue in privateKey){
        val pagaiduValue = (keyValue * inversMod).mod(primNumb)
        //println("($keyValue * $inversMod) mod $primNumb = $pagaiduValue")
        publicKey.add(pagaiduValue)
    }
    return publicKey 
}

1 Answer 1

3

Here's a short and simple solution:

inline fun <T> Iterable<T>.times(count: Int) = (1..count).flatMap { this }

Can be used like this:

val numbers = listOf(1, 2, 3)
val result = numbers.times(5)
println(result)                 // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

Perhaps the naming could use some work, times could also be assumed to have the effect of multiplying each element by a given number, repeat could be a better alternative. Also, you might wanna define it on Array<T> and the primitive array types (IntArray, etc) as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.