1

I am working on Monoalphabetic Cipher and I have to encrypt a message by substituting it with any other alphabet.

fun encrypt(message:String){
//Assume message as ABCD
var encrypt:String = ""

val alphabets = ('A'..'Z').toMutableList() //['A','B','C','D',....]

var shuffAlp = alphabets.shuffled() // Assume shuffled op as ['D','L','N','J',....]

var pair = alphabets.zip(shuffAlp)
println(pair)   // Assume pair as [(A, D), (B, L), (C, N), (D, J), ...] acc to shuffled op

for(m in message){
    for (p in pair){
        if(p.first == m)
            encrypt+=p.second
        }
    }
println(encrypt) //DLNJ according to given pair
}

As you can see I have used two for loops to generate my encrypt String. I was wondering if there is an equivalent Lambda function or extensions that would discard two loops?

Silver Sky
  • 431
  • 2
  • 12
  • If message had repeated characters and zipping allot different character pairs then how would you tackle that, for example if message is `"ABCA"`, then `(A,D)` and `(A,E)` maybe present so what do you want it to replace? with current code it will be `DELNDE` – Animesh Sahu Aug 09 '20 at 15:12
  • the `message` is not being mapped with `shuffAlp`, `alphabets` is being mapped with `shuffAlp`. Once you read Monoalphabetic Cipher this code will make more sense and you might even come up with a better solution. – Silver Sky Aug 13 '20 at 16:41

1 Answers1

4

After zipping, use toMap so you can look up the encrypted values easily without looping.

You're using vars where your variables can be constant vals, and MutableList as an unnecessary intermediate step.

// Assumes message contains only capital letters
fun encrypt (message: String): String {
    val alphabet = 'A'..'Z'
    val cipher = alphabet.zip(alphabet.shuffled()).toMap()
    return message.map { cipher[it] }.joinToString("")
}

But if you want a message that you can decrypt later, you need separate functions so you can store the cipher and reuse it. Something like this:

fun createCipher(): Map<Char, Char> {
    val alphabet = 'A'..'Z'
    return alphabet.zip(alphabet.shuffled()).toMap()
}

fun encrypt(message: String, cipher: Map<Char, Char>): String {
    return message.map { cipher[it] }.joinToString("")
}

fun decrypt(encrpytedMessage: String, cipher: Map<Char, Char>): String {
    val invertedCipher = cipher.entries.associate { it.value to it.key }
    return encrpytedMessage.map { invertedCipher [it] }.joinToString("")
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154