Sort by

recency

|

1475 Discussions

|

  • + 0 comments

    Haskell:

    module Main where
    
    import Data.List (transpose)
    
    solve :: String -> String
    solve plain = encrypted
      where
        plain' = filter (/= ' ') plain
        len = length plain'
        rows = floor . sqrt $ fromIntegral len
        cols = ceiling . sqrt $ fromIntegral len
        grid = [take cols $ drop (i * cols) plain' | i <- [0 .. rows]]
        grid' = filter (/= "") grid -- edge case, final is ""
        encrypted = unwords $ transpose grid
    
    main :: IO ()
    main = interact solve
    
  • + 0 comments

    public static String encryption(String s) { StringBuilder result = new StringBuilder(); s = s.replaceAll("\s", ""); int n = s.length(); int row = (int) Math.sqrt(n); int column = (row * row == n) ? row : row + 1; for (int i = 0; i < column; i++) { for (int j = i; j < n; j += column) { result.append(s.charAt(j)); } result.append(" ");

        }
    
        return result.toString();
    
    }
    
  • + 0 comments

    public static String encryption(String s) { int len = s.length(); int row = (int) Math.floor(Math.sqrt(len)); int col = (int) Math.ceil(Math.sqrt(len));

        if (row * col < len) {
            row++;
        }
    
    
        char[][] matrix = new char[row][col];
    
    
        int index = 0;
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (index < len) {
                    matrix[r][c] = s.charAt(index++);
                } else {
                    matrix[r][c] = ' '; 
                }
            }
        }
    
    
        StringBuilder sb = new StringBuilder();
        for (int c = 0; c < col; c++) {
            if (c > 0) sb.append(" ");
            for (int r = 0; r < row; r++) {
                if (matrix[r][c] != ' ') {
                    sb.append(matrix[r][c]);
                }
            }
        }
    
            return sb.toString();
    
    
    }
    
    • }
  • + 0 comments

    Kotlin proposal

    import java.io.*
    import java.math.*
    import java.security.*
    import java.text.*
    import java.util.*
    import java.util.concurrent.*
    import java.util.function.*
    import java.util.regex.*
    import java.util.stream.*
    import kotlin.collections.*
    import kotlin.comparisons.*
    import kotlin.io.*
    import kotlin.jvm.*
    import kotlin.jvm.functions.*
    import kotlin.jvm.internal.*
    import kotlin.ranges.*
    import kotlin.sequences.*
    import kotlin.text.*
    
    /*
     * Complete the 'encryption' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    fun encryption(s: String): String {
        val trimmed: String = s.replace(" ", "")
        val lengthTrimmed = trimmed.length
        val sqrt: Double = Math.sqrt(lengthTrimmed.toDouble())
        
        var rows = sqrt.toInt()
        var columns = if(sqrt % rows == 0.0) { // Cuadratico we
            rows
        } else { // Rectangular we
            rows + 1
        }
        
        if(rows * columns < lengthTrimmed){
            rows++
        }
        
        val words = mutableListOf<String>()
        for(r in 0 until rows){
            words.add(getSubstringSegment(trimmed, r, columns))
        }
        
        return transformWordsList(words, columns).joinToString(" ")
    
    }
    
    private fun transformWordsList(words: List<String>, eachWordSize: Int): List<String> {
        
        val result = mutableListOf<String>()
        for(y in 0 until eachWordSize){
            val sb = StringBuilder("")
            for(x in 0 until words.size){
                if(y < words.get(x).length) {
                    sb.append("${words.get(x)[y]}")
                }
            }
            result.add(sb.toString())
        }
        return result
    }
    
    // (7, 8) 0 -> 0,7; 1 -> 8, 15; 2 -> 16, 23
    private fun getSubstringSegment(s: String, row: Int, eachWordSize: Int): String {
        val startIndexInc = row * eachWordSize
        val endIndexExc = startIndexInc + eachWordSize
        return s.substring(startIndexInc, if(endIndexExc > s.length) s.length else endIndexExc)
    }
    
    fun main(args: Array<String>) {
        val s = readLine()!!
    
        val result = encryption(s)
    
        println(result)
    }
    
  • + 0 comments

    The solution for "chillout" seems to be wrong?? The length = 8, which translates to 2X3, if we need to store all 8 characters, the array can be 2X4 or 3X3. According to the problem the smallest area should be chosen, which is 2X4, but the example shows 3X3??