We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*importjava.math.*importjava.security.*importjava.text.*importjava.util.*importjava.util.concurrent.*importjava.util.function.*importjava.util.regex.*importjava.util.stream.*importkotlin.collections.*importkotlin.comparisons.*importkotlin.io.*importkotlin.jvm.*importkotlin.jvm.functions.*importkotlin.jvm.internal.*importkotlin.ranges.*importkotlin.sequences.*importkotlin.text.*/* * Complete the 'encryption' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */funencryption(s:String):String{valtrimmed:String=s.replace(" ","")vallengthTrimmed=trimmed.lengthvalsqrt:Double=Math.sqrt(lengthTrimmed.toDouble())varrows=sqrt.toInt()varcolumns=if(sqrt%rows==0.0){// Cuadratico werows}else{// Rectangular werows+1}if(rows*columns<lengthTrimmed){rows++}valwords=mutableListOf<String>()for(rin0untilrows){words.add(getSubstringSegment(trimmed,r,columns))}returntransformWordsList(words,columns).joinToString(" ")}privatefuntransformWordsList(words:List<String>,eachWordSize:Int):List<String>{valresult=mutableListOf<String>()for(yin0untileachWordSize){valsb=StringBuilder("")for(xin0untilwords.size){if(y<words.get(x).length){sb.append("${words.get(x)[y]}")}}result.add(sb.toString())}returnresult}// (7, 8) 0 -> 0,7; 1 -> 8, 15; 2 -> 16, 23privatefungetSubstringSegment(s:String,row:Int,eachWordSize:Int):String{valstartIndexInc=row*eachWordSizevalendIndexExc=startIndexInc+eachWordSizereturns.substring(startIndexInc,if(endIndexExc>s.length)s.lengthelseendIndexExc)}funmain(args:Array<String>){vals=readLine()!!valresult=encryption(s)println(result)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →
Kotlin proposal