Sort by

recency

|

1501 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    /*
     * Complete the 'encryption' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    public static String encryption(String s) {
        // Remove spaces
        s = s.replaceAll("\\s", "");
        int len = s.length();
    
        // Calculate rows and columns
        int rows = (int) Math.floor(Math.sqrt(len));
        int cols = (int) Math.ceil(Math.sqrt(len));
        if (rows * cols < len) {
            rows++;
        }
    
        // Build the encrypted string
        StringBuilder encrypted = new StringBuilder();
        for (int c = 0; c < cols; c++) {
            for (int r = 0; r < rows; r++) {
                int index = r * cols + c;
                if (index < len) {
                    encrypted.append(s.charAt(index));
                }
            }
            encrypted.append(" ");
        }
    
        return encrypted.toString().trim();
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();
    
        String result = Result.encryption(s);
    
        bufferedWriter.write(result);
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments
    def encryption(s):
        # Write your code here
        n = len(s)
        x = math.sqrt(n)
        # nr = math.floor(1x)
        nc = math.ceil(x)
        # print(n, nc)
    
        i = 0
        j = 0
        q = []
        for _ in range(n):
            if i+j > n-1:
                q.append(' ')
                j += 1
                i = 0
            q.append(s[i+j])
            i += nc
            
        return ''.join(q)
    
  • + 0 comments
    def encryption(s):
        s = s.replace(" ", "")
        l = len(s)
        sqrt = math.sqrt(l)
        cols = math.ceil(sqrt)
        rows = math.floor(sqrt)
        if rows * cols < l:
            rows = cols
            
        split_str = []
        for i in range(0, l, cols):
            split_str.append(s[i: i+cols])
        
        return_str = ""
        rows_count = 0
        cols_count = 0
        while cols_count < cols:
            while rows_count < rows and cols_count < len(split_str[rows_count]):
                return_str += split_str[rows_count][cols_count]
                rows_count += 1
            rows_count = 0
            return_str += " "
            cols_count += 1
            
        return return_str
    
  • + 1 comment

    I've failed in the word "chillout". This word has 8 letters, so, it can take 3x3 and 2x4 matrix formats. The 2x4 is the one with a smaller area. My final answer was: "cl ho iu lt". But the validation routine says "clu hlt io", like it was 3x3 matrix.

    Did you have problemns with this test too?

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-encryption-problem-solution.html