我正在研究单字母密码,我必须通过用任何其他字母代替它来加密消息。
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
}
如您所见,我使用了两个 for 循环来生成我的加密字符串。我想知道是否有一个等效的 Lambda 函数或扩展会丢弃两个循环?